KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > transaction > jta > JtaTransaction


1 /*
2  * Copyright 2004 Clinton Begin
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 package com.ibatis.sqlmap.engine.transaction.jta;
17
18 import com.ibatis.common.jdbc.logging.ConnectionLogProxy;
19 import com.ibatis.common.logging.Log;
20 import com.ibatis.common.logging.LogFactory;
21 import com.ibatis.sqlmap.engine.transaction.IsolationLevel;
22 import com.ibatis.sqlmap.engine.transaction.Transaction;
23 import com.ibatis.sqlmap.engine.transaction.TransactionException;
24
25 import javax.sql.DataSource JavaDoc;
26 import javax.transaction.Status JavaDoc;
27 import javax.transaction.UserTransaction JavaDoc;
28 import java.sql.Connection JavaDoc;
29 import java.sql.SQLException JavaDoc;
30
31 public class JtaTransaction implements Transaction {
32
33   private static final Log connectionLog = LogFactory.getLog(Connection JavaDoc.class);
34
35   private UserTransaction JavaDoc userTransaction;
36   private DataSource JavaDoc dataSource;
37   private Connection JavaDoc connection;
38   private IsolationLevel isolationLevel = new IsolationLevel();
39
40   private boolean commmitted = false;
41   private boolean newTransaction = false;
42
43   public JtaTransaction(UserTransaction JavaDoc utx, DataSource JavaDoc ds, int isolationLevel) throws TransactionException {
44     // Check parameters
45
userTransaction = utx;
46     dataSource = ds;
47     if (userTransaction == null) {
48       throw new TransactionException("JtaTransaction initialization failed. UserTransaction was null.");
49     }
50     if (dataSource == null) {
51       throw new TransactionException("JtaTransaction initialization failed. DataSource was null.");
52     }
53     this.isolationLevel.setIsolationLevel(isolationLevel);
54   }
55
56   private void init() throws TransactionException, SQLException JavaDoc {
57     // Start JTA Transaction
58
try {
59       newTransaction = userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION;
60       if (newTransaction) {
61         userTransaction.begin();
62       }
63     } catch (Exception JavaDoc e) {
64       throw new TransactionException("JtaTransaction could not start transaction. Cause: ", e);
65     }
66
67     // Open JDBC Connection
68
connection = dataSource.getConnection();
69     if (connection == null) {
70       throw new TransactionException("JtaTransaction could not start transaction. Cause: The DataSource returned a null connection.");
71     }
72     // Isolation Level
73
isolationLevel.applyIsolationLevel(connection);
74     // AutoCommit
75
if (connection.getAutoCommit()) {
76       connection.setAutoCommit(false);
77     }
78     // Debug
79
if (connectionLog.isDebugEnabled()) {
80       connection = ConnectionLogProxy.newInstance(connection);
81     }
82   }
83
84   public void commit() throws SQLException JavaDoc, TransactionException {
85     if (connection != null) {
86       if (commmitted) {
87         throw new TransactionException("JtaTransaction could not commit because this transaction has already been committed.");
88       }
89       try {
90         if (newTransaction) {
91           userTransaction.commit();
92         }
93       } catch (Exception JavaDoc e) {
94         throw new TransactionException("JtaTransaction could not commit. Cause: ", e);
95       }
96       commmitted = true;
97     }
98   }
99
100   public void rollback() throws SQLException JavaDoc, TransactionException {
101     if (connection != null) {
102       if (!commmitted) {
103         try {
104           if (userTransaction != null) {
105             if (newTransaction) {
106               userTransaction.rollback();
107             } else {
108               userTransaction.setRollbackOnly();
109             }
110           }
111         } catch (Exception JavaDoc e) {
112           throw new TransactionException("JtaTransaction could not rollback. Cause: ", e);
113         }
114       }
115     }
116   }
117
118   public void close() throws SQLException JavaDoc, TransactionException {
119     if (connection != null) {
120       try {
121         isolationLevel.restoreIsolationLevel(connection);
122       } finally {
123         connection.close();
124         connection = null;
125       }
126     }
127   }
128
129   public Connection JavaDoc getConnection() throws SQLException JavaDoc, TransactionException {
130     if (connection == null) {
131       init();
132     }
133     return connection;
134   }
135
136
137 }
138
Popular Tags