1 | /** |
2 | * Copyright 2009 ATG DUST Project |
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 | * |
7 | * You may obtain a copy of the License at 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 limitations under the License. |
13 | */ |
14 | |
15 | package atg.adapter.gsa; |
16 | |
17 | import java.sql.Connection; |
18 | import java.sql.SQLException; |
19 | |
20 | import javax.transaction.InvalidTransactionException; |
21 | import javax.transaction.SystemException; |
22 | import javax.transaction.Transaction; |
23 | |
24 | import org.apache.log4j.Logger; |
25 | import org.apache.log4j.Priority; |
26 | |
27 | class DoInAutoCommit { |
28 | |
29 | private final GSARepositorySchemaGenerator mAutoCommit; |
30 | GSARepository mRepository = null; |
31 | Logger mLogger = Logger.getLogger(this.getClass()); |
32 | |
33 | /** |
34 | * Creates a new DoInAutoCommit which operates on the given repository. |
35 | * This class is used to allow code to be executed with autoCommit=true on |
36 | * its database connection. This class will suspend the current transaction |
37 | * if any before setting autoCommit=true. The transaction is resumed and |
38 | * autoCommit is returned to its original state after work is performed. |
39 | * |
40 | * @param pRepository |
41 | * @param pGsaRepositorySchemaGenerator TODO |
42 | */ |
43 | public DoInAutoCommit(GSARepositorySchemaGenerator pGsaRepositorySchemaGenerator, GSARepository pRepository) { |
44 | mAutoCommit = pGsaRepositorySchemaGenerator; |
45 | mRepository = pRepository; |
46 | } |
47 | |
48 | /** |
49 | * Executes the given work using the connections and logging of the |
50 | * repository passed into the constructor of this class. Returns true if the |
51 | * work was competed without any exceptions. |
52 | * |
53 | * @param pWork |
54 | */ |
55 | public boolean doInAutoCommit(AutoCommitable pWork) { |
56 | Transaction suspended = null; |
57 | boolean success = false; |
58 | try { |
59 | // Suspend the Current Transaction so we can set autoCommit=true |
60 | // Otherwise MSSQL will hang |
61 | suspended = mRepository.getTransactionManager().suspend(); |
62 | Connection c = mRepository.getConnection(); |
63 | if (mLogger.isDebugEnabled()) |
64 | mLogger.log(Priority.DEBUG,"autoCommit = " + c.getAutoCommit() |
65 | + " connection=" + c); |
66 | boolean savedAutoCommit = c.getAutoCommit(); |
67 | if (mLogger.isDebugEnabled()) |
68 | mLogger.log(Priority.DEBUG,"Setting auto commit = true on connection " + c); |
69 | c.setAutoCommit(true); |
70 | try { |
71 | pWork.doInAutoCommit(c); |
72 | success = true; |
73 | } finally { |
74 | if (c != null) { |
75 | if (mLogger.isDebugEnabled()) |
76 | mLogger.log(Priority.DEBUG,"Reverting autoCommit back to " |
77 | + savedAutoCommit); |
78 | c.setAutoCommit(savedAutoCommit); |
79 | } |
80 | if (suspended != null) { |
81 | try { |
82 | mRepository.getTransactionManager().resume(suspended); |
83 | } catch (InvalidTransactionException e) { |
84 | if (mRepository.isLoggingError()) |
85 | mLogger.log(Priority.ERROR,e); |
86 | } catch (IllegalStateException e) { |
87 | if (mRepository.isLoggingError()) |
88 | mLogger.log(Priority.ERROR,e); |
89 | } |
90 | } |
91 | } |
92 | } catch (SystemException e) { |
93 | if (mRepository.isLoggingError()) |
94 | mLogger.log(Priority.ERROR,e); |
95 | } catch (SQLException e) { |
96 | if (mRepository.isLoggingError()) |
97 | mLogger.log(Priority.ERROR,e); |
98 | } |
99 | return success; |
100 | } |
101 | } |