KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > transaction > jdbc > JdbcTransaction


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