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 | 0 | 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 | 0 | public TransactionalLifecycle(String transactionManagerName) { |
44 | 0 | this.transactionManagerName = transactionManagerName; |
45 | 0 | } |
46 | |
|
47 | |
public TransactionalLifecycle() { |
48 | 0 | this(DEFAULT_TRANSACTION_MANAGER_NAME); |
49 | 0 | } |
50 | |
|
51 | |
public void setTransactionManager(PlatformTransactionManager transactionManager) { |
52 | 0 | this.transactionManager = transactionManager; |
53 | 0 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
private PlatformTransactionManager getTransactionManager() { |
61 | 0 | if (transactionManager == null) { |
62 | 0 | transactionManager = (PlatformTransactionManager) GlobalResourceLoader.getService(transactionManagerName); |
63 | |
} |
64 | 0 | return transactionManager; |
65 | |
} |
66 | |
|
67 | |
private boolean started; |
68 | |
private TransactionStatus TRANSACTION_STATUS; |
69 | |
|
70 | |
public boolean isStarted() { |
71 | 0 | return started; |
72 | |
} |
73 | |
|
74 | |
public void start() throws Exception { |
75 | 0 | LOG.info("Starting a transaction from TransactionalLifecycle..."); |
76 | 0 | DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition(); |
77 | 0 | defaultTransactionDefinition.setTimeout(3600); |
78 | 0 | TRANSACTION_STATUS = getTransactionManager().getTransaction(defaultTransactionDefinition); |
79 | 0 | started = true; |
80 | 0 | } |
81 | |
|
82 | |
public void stop() throws Exception { |
83 | 0 | LOG.info("...rolling back transaction from TransactionalLifecycle."); |
84 | 0 | getTransactionManager().rollback(TRANSACTION_STATUS); |
85 | 0 | started = false; |
86 | 0 | } |
87 | |
|
88 | |
} |