1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.test;
17
18 import org.kuali.rice.core.api.lifecycle.Lifecycle;
19 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
20 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21 import org.springframework.transaction.PlatformTransactionManager;
22 import org.springframework.transaction.TransactionStatus;
23 import org.springframework.transaction.support.DefaultTransactionDefinition;
24
25
26
27
28
29 public class TransactionalLifecycle implements Lifecycle {
30
31 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
32 .getLogger(TransactionalLifecycle.class);
33
34
35
36
37
38 public static final String DEFAULT_TRANSACTION_MANAGER_NAME = "transactionManager";
39
40 private String transactionManagerName;
41 private PlatformTransactionManager transactionManager;
42
43 public TransactionalLifecycle(String transactionManagerName) {
44 this.transactionManagerName = transactionManagerName;
45 }
46
47 public TransactionalLifecycle() {
48 this(DEFAULT_TRANSACTION_MANAGER_NAME);
49 }
50
51 public void setTransactionManager(PlatformTransactionManager transactionManager) {
52 this.transactionManager = transactionManager;
53 }
54
55
56
57
58
59
60 private PlatformTransactionManager getTransactionManager() {
61 if (transactionManager == null) {
62 transactionManager = (PlatformTransactionManager) GlobalResourceLoader.getService(transactionManagerName);
63 }
64 return transactionManager;
65 }
66
67 private boolean started;
68 private TransactionStatus TRANSACTION_STATUS;
69
70 public boolean isStarted() {
71 return started;
72 }
73
74 public void start() throws Exception {
75 LOG.info("Starting a transaction from TransactionalLifecycle...");
76 DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
77 defaultTransactionDefinition.setTimeout(3600);
78 TRANSACTION_STATUS = getTransactionManager().getTransaction(defaultTransactionDefinition);
79 started = true;
80 }
81
82 public void stop() throws Exception {
83 LOG.info("...rolling back transaction from TransactionalLifecycle.");
84 getTransactionManager().rollback(TRANSACTION_STATUS);
85 started = false;
86 }
87
88 }