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.hibernate3; |
16 | |
17 | import org.apache.commons.logging.Log; |
18 | |
19 | import net.sourceforge.hivetranse.transaction.AbstractTransactionService; |
20 | import net.sourceforge.hiveutils.collections.Stack; |
21 | import net.sourceforge.hiveutils.collections.impl.StackImpl; |
22 | |
23 | /** |
24 | * Service managing Hibernate sessions & transactions, without JTA. |
25 | * This service works hand-in-hand with <code>SessionProxyFactory</code>. |
26 | * <p> |
27 | * The service supports more than one <code>SessionFactory</code> with the |
28 | * limitation that partial commit or rollback may occur across different |
29 | * databases, because JTA is not used. |
30 | * <p> |
31 | * If you want correct management of several databases, then better try using |
32 | * <code>JtaTransactionService</code>. |
33 | * <p> |
34 | * <b>ServiceModel must be threaded</b> |
35 | * |
36 | * @author Jean-Francois Poilpret |
37 | */ |
38 | public class HibernateTransactionService extends AbstractTransactionService |
39 | { |
40 | public HibernateTransactionService( Log logger, |
41 | boolean wrapRuntimeExceptions, |
42 | boolean deferSessionClose, |
43 | boolean reuseLastSession) |
44 | { |
45 | super(logger, wrapRuntimeExceptions); |
46 | _deferSessionClose = deferSessionClose; |
47 | if (_deferSessionClose) |
48 | { |
49 | _sessionsRepositories = new StackImpl<SessionsRepository>(); |
50 | } |
51 | else |
52 | { |
53 | _sessionsRepositories = null; |
54 | } |
55 | } |
56 | |
57 | protected Object createTransaction() throws Exception |
58 | { |
59 | return new SessionsRepositoryImpl(); |
60 | } |
61 | |
62 | protected void endTransaction(Object tx, boolean commit) |
63 | throws Exception |
64 | { |
65 | SessionsRepository repo = (SessionsRepository) tx; |
66 | if (_deferSessionClose) |
67 | { |
68 | _sessionsRepositories.push(repo); |
69 | } |
70 | repo.endAllSessions(!_deferSessionClose, commit); |
71 | } |
72 | |
73 | protected void cleanUp() |
74 | { |
75 | super.cleanUp(); |
76 | if (_deferSessionClose) |
77 | { |
78 | while (!_sessionsRepositories.isEmpty()) |
79 | { |
80 | SessionsRepository repo = _sessionsRepositories.pop(); |
81 | //#### should we commit instead? |
82 | repo.endAllSessions(true, false); |
83 | } |
84 | } |
85 | } |
86 | |
87 | private final boolean _deferSessionClose; |
88 | private final Stack<SessionsRepository> _sessionsRepositories; |
89 | } |