KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > dao > engine > transaction > jta > JtaDaoTransaction


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