1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hivetranse.transaction.jdbc; |
16 | |
17 | import java.sql.SQLException; |
18 | |
19 | import org.apache.commons.logging.Log; |
20 | |
21 | import net.sourceforge.hivetranse.transaction.AbstractTransactionService; |
22 | import net.sourceforge.hivetranse.transaction.TransactionException; |
23 | import net.sourceforge.hiveutils.collections.Stack; |
24 | import net.sourceforge.hiveutils.collections.impl.StackImpl; |
25 | |
26 | /** |
27 | * Service managing JDBC transactions. |
28 | * This service works hand-in-hand with <code>ConnectionProxyFactory</code> |
29 | * to handle different transactions on the same DataSource if necessary. |
30 | * <p> |
31 | * Only one DataSource can have a transaction (XA transactions are not supported |
32 | * in pure JDBC, without JTA). This DataSource <b>MUST BE</b> pooled. |
33 | * <p> |
34 | * This service does not only handle JDBC connections but it also does all the |
35 | * work related to Connection creation from a DataSource; the current |
36 | * Connectiont is stored on <code>ThreadLocalStorage</code>. |
37 | * <p> |
38 | * <b>ServiceModel must be threaded</b> |
39 | * |
40 | * @author Jean-Francois Poilpret |
41 | */ |
42 | public class JdbcTransactionService extends AbstractTransactionService |
43 | { |
44 | public JdbcTransactionService( Log logger, |
45 | boolean wrapRuntimeExceptions, |
46 | boolean deferConnectionClose) |
47 | { |
48 | super(logger, wrapRuntimeExceptions); |
49 | _deferConnectionClose = deferConnectionClose; |
50 | if (_deferConnectionClose) |
51 | { |
52 | _connectionsRepositories = new StackImpl<ConnectionsRepository>(); |
53 | } |
54 | else |
55 | { |
56 | _connectionsRepositories = null; |
57 | } |
58 | } |
59 | |
60 | protected Object createTransaction() throws Exception |
61 | { |
62 | return new ConnectionsRepositoryImpl(); |
63 | } |
64 | |
65 | protected void endTransaction(Object tx, boolean commit) |
66 | throws Exception |
67 | { |
68 | ConnectionsRepository repo = (ConnectionsRepository) tx; |
69 | if (_deferConnectionClose) |
70 | { |
71 | _connectionsRepositories.push(repo); |
72 | } |
73 | repo.endAllConnections(!_deferConnectionClose, commit); |
74 | } |
75 | |
76 | protected void cleanUp() |
77 | { |
78 | super.cleanUp(); |
79 | if (_deferConnectionClose) |
80 | { |
81 | try |
82 | { |
83 | while (!_connectionsRepositories.isEmpty()) |
84 | { |
85 | ConnectionsRepository repo = _connectionsRepositories.pop(); |
86 | //#### should we commit instead? |
87 | repo.endAllConnections(true, false); |
88 | } |
89 | } |
90 | catch (SQLException e) |
91 | { |
92 | throw new TransactionException(e); |
93 | } |
94 | } |
95 | } |
96 | |
97 | private final boolean _deferConnectionClose; |
98 | private final Stack<ConnectionsRepository> _connectionsRepositories; |
99 | } |