01: /*
02: * Copyright 2004 Clinton Begin
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
26: import java.sql.Connection;
27: import java.sql.SQLException;
28:
29: public class JdbcTransaction implements Transaction {
30:
31: private static final Log connectionLog = LogFactory
32: .getLog(Connection.class);
33:
34: private DataSource dataSource;
35: private Connection connection;
36: private IsolationLevel isolationLevel = new IsolationLevel();
37:
38: public JdbcTransaction(DataSource ds, int isolationLevel)
39: throws TransactionException {
40: // Check Parameters
41: dataSource = ds;
42: if (dataSource == null) {
43: throw new TransactionException(
44: "JdbcTransaction initialization failed. DataSource was null.");
45: }
46: this .isolationLevel.setIsolationLevel(isolationLevel);
47: }
48:
49: private void init() throws SQLException, TransactionException {
50: // Open JDBC Transaction
51: connection = dataSource.getConnection();
52: if (connection == null) {
53: throw new TransactionException(
54: "JdbcTransaction could not start transaction. Cause: The DataSource returned a null connection.");
55: }
56: // Isolation Level
57: isolationLevel.applyIsolationLevel(connection);
58: // AutoCommit
59: if (connection.getAutoCommit()) {
60: connection.setAutoCommit(false);
61: }
62: // Debug
63: if (connectionLog.isDebugEnabled()) {
64: connection = ConnectionLogProxy.newInstance(connection);
65: }
66: }
67:
68: public void commit() throws SQLException, TransactionException {
69: if (connection != null) {
70: connection.commit();
71: }
72: }
73:
74: public void rollback() throws SQLException, TransactionException {
75: if (connection != null) {
76: connection.rollback();
77: }
78: }
79:
80: public void close() throws SQLException, TransactionException {
81: if (connection != null) {
82: try {
83: isolationLevel.restoreIsolationLevel(connection);
84: } finally {
85: connection.close();
86: connection = null;
87: }
88: }
89: }
90:
91: public Connection getConnection() throws SQLException,
92: TransactionException {
93: if (connection == null) {
94: init();
95: }
96: return connection;
97: }
98:
99: }
|