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 | protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException { |
30 | ((ResourcelessTransaction) transaction).begin(); |
31 | } |
32 | |
33 | protected void doCommit(DefaultTransactionStatus status) throws TransactionException { |
34 | logger.debug("Committing resourceless transaction on [" + status.getTransaction() + "]"); |
35 | } |
36 | |
37 | protected Object doGetTransaction() throws TransactionException { |
38 | Object transaction = new ResourcelessTransaction(); |
39 | Stack<Object> resources; |
40 | if (!TransactionSynchronizationManager.hasResource(this)) { |
41 | resources = new Stack<Object>(); |
42 | TransactionSynchronizationManager.bindResource(this, resources); |
43 | } |
44 | else { |
45 | @SuppressWarnings("unchecked") |
46 | Stack<Object> stack = (Stack<Object>) TransactionSynchronizationManager.getResource(this); |
47 | resources = stack; |
48 | } |
49 | resources.push(transaction); |
50 | return transaction; |
51 | } |
52 | |
53 | protected void doRollback(DefaultTransactionStatus status) throws TransactionException { |
54 | logger.debug("Rolling back resourceless transaction on [" + status.getTransaction() + "]"); |
55 | } |
56 | |
57 | protected boolean isExistingTransaction(Object transaction) throws TransactionException { |
58 | if (TransactionSynchronizationManager.hasResource(this)) { |
59 | @SuppressWarnings("unchecked") |
60 | Stack<Object> stack = (Stack<Object>) TransactionSynchronizationManager.getResource(this); |
61 | return stack.size() > 1; |
62 | } |
63 | return ((ResourcelessTransaction) transaction).isActive(); |
64 | } |
65 | |
66 | protected void doSetRollbackOnly(DefaultTransactionStatus status) throws TransactionException { |
67 | } |
68 | |
69 | protected void doCleanupAfterCompletion(Object transaction) { |
70 | @SuppressWarnings("unchecked") |
71 | Stack<Object> list = (Stack<Object>) TransactionSynchronizationManager.getResource(this); |
72 | Stack<Object> resources = list; |
73 | resources.clear(); |
74 | TransactionSynchronizationManager.unbindResource(this); |
75 | ((ResourcelessTransaction) transaction).clear(); |
76 | } |
77 | |
78 | private static class ResourcelessTransaction { |
79 | |
80 | private boolean active = false; |
81 | |
82 | public boolean isActive() { |
83 | return active; |
84 | } |
85 | |
86 | public void begin() { |
87 | active = true; |
88 | } |
89 | |
90 | public void clear() { |
91 | active = false; |
92 | } |
93 | |
94 | } |
95 | |
96 | } |