KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > transaction > external > ExternalTransaction


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.external;
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 java.sql.Connection JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 public class ExternalTransaction implements Transaction {
30
31   private static final Log connectionLog = LogFactory.getLog(Connection JavaDoc.class);
32
33   private DataSource JavaDoc dataSource;
34   private boolean defaultAutoCommit;
35   private boolean setAutoCommitAllowed;
36   private Connection JavaDoc connection;
37   private IsolationLevel isolationLevel = new IsolationLevel();
38
39   public ExternalTransaction(DataSource JavaDoc ds, boolean defaultAutoCommit, boolean setAutoCommitAllowed, int isolationLevel) throws TransactionException {
40     // Check Parameters
41
dataSource = ds;
42     if (dataSource == null) {
43       throw new TransactionException("ExternalTransaction initialization failed. DataSource was null.");
44     }
45
46     this.defaultAutoCommit = defaultAutoCommit;
47     this.setAutoCommitAllowed = setAutoCommitAllowed;
48     this.isolationLevel.setIsolationLevel(isolationLevel);
49   }
50
51   private void init() throws SQLException JavaDoc, TransactionException {
52     // Open JDBC Transaction
53
connection = dataSource.getConnection();
54     if (connection == null) {
55       throw new TransactionException("ExternalTransaction could not start transaction. Cause: The DataSource returned a null connection.");
56     }
57     // Isolation Level
58
isolationLevel.applyIsolationLevel(connection);
59     // AutoCommit
60
if (setAutoCommitAllowed) {
61       if (connection.getAutoCommit() != defaultAutoCommit) {
62         connection.setAutoCommit(defaultAutoCommit);
63       }
64     }
65     // Debug
66
if (connectionLog.isDebugEnabled()) {
67       connection = ConnectionLogProxy.newInstance(connection);
68     }
69   }
70
71   public void commit() throws SQLException JavaDoc, TransactionException {
72   }
73
74   public void rollback() throws SQLException JavaDoc, TransactionException {
75   }
76
77   public void close() throws SQLException JavaDoc, TransactionException {
78     if (connection != null) {
79       try {
80         isolationLevel.restoreIsolationLevel(connection);
81       } finally {
82         connection.close();
83         connection = null;
84       }
85     }
86   }
87
88   public Connection JavaDoc getConnection() throws SQLException JavaDoc, TransactionException {
89     if (connection == null) {
90       init();
91     }
92     return connection;
93   }
94
95 }
96
Popular Tags