1 | /* |
2 | * Copyright 2006-2007 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | package org.springframework.batch.support.transaction; |
18 | |
19 | import java.util.Stack; |
20 | |
21 | import org.springframework.transaction.TransactionDefinition; |
22 | import org.springframework.transaction.TransactionException; |
23 | import org.springframework.transaction.support.AbstractPlatformTransactionManager; |
24 | import org.springframework.transaction.support.DefaultTransactionStatus; |
25 | import org.springframework.transaction.support.TransactionSynchronizationManager; |
26 | |
27 | public class ResourcelessTransactionManager extends AbstractPlatformTransactionManager { |
28 | |
29 | @Override |
30 | protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException { |
31 | ((ResourcelessTransaction) transaction).begin(); |
32 | } |
33 | |
34 | @Override |
35 | protected void doCommit(DefaultTransactionStatus status) throws TransactionException { |
36 | logger.debug("Committing resourceless transaction on [" + status.getTransaction() + "]"); |
37 | } |
38 | |
39 | @Override |
40 | protected Object doGetTransaction() throws TransactionException { |
41 | Object transaction = new ResourcelessTransaction(); |
42 | Stack<Object> resources; |
43 | if (!TransactionSynchronizationManager.hasResource(this)) { |
44 | resources = new Stack<Object>(); |
45 | TransactionSynchronizationManager.bindResource(this, resources); |
46 | } |
47 | else { |
48 | @SuppressWarnings("unchecked") |
49 | Stack<Object> stack = (Stack<Object>) TransactionSynchronizationManager.getResource(this); |
50 | resources = stack; |
51 | } |
52 | resources.push(transaction); |
53 | return transaction; |
54 | } |
55 | |
56 | @Override |
57 | protected void doRollback(DefaultTransactionStatus status) throws TransactionException { |
58 | logger.debug("Rolling back resourceless transaction on [" + status.getTransaction() + "]"); |
59 | } |
60 | |
61 | @Override |
62 | protected boolean isExistingTransaction(Object transaction) throws TransactionException { |
63 | if (TransactionSynchronizationManager.hasResource(this)) { |
64 | @SuppressWarnings("unchecked") |
65 | Stack<Object> stack = (Stack<Object>) TransactionSynchronizationManager.getResource(this); |
66 | return stack.size() > 1; |
67 | } |
68 | return ((ResourcelessTransaction) transaction).isActive(); |
69 | } |
70 | |
71 | @Override |
72 | protected void doSetRollbackOnly(DefaultTransactionStatus status) throws TransactionException { |
73 | } |
74 | |
75 | @Override |
76 | protected void doCleanupAfterCompletion(Object transaction) { |
77 | @SuppressWarnings("unchecked") |
78 | Stack<Object> list = (Stack<Object>) TransactionSynchronizationManager.getResource(this); |
79 | Stack<Object> resources = list; |
80 | resources.clear(); |
81 | TransactionSynchronizationManager.unbindResource(this); |
82 | ((ResourcelessTransaction) transaction).clear(); |
83 | } |
84 | |
85 | private static class ResourcelessTransaction { |
86 | |
87 | private boolean active = false; |
88 | |
89 | public boolean isActive() { |
90 | return active; |
91 | } |
92 | |
93 | public void begin() { |
94 | active = true; |
95 | } |
96 | |
97 | public void clear() { |
98 | active = false; |
99 | } |
100 | |
101 | } |
102 | |
103 | } |