001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.sqlmap.engine.transaction.external;
017:
018: import com.ibatis.common.jdbc.logging.ConnectionLogProxy;
019: import com.ibatis.common.logging.Log;
020: import com.ibatis.common.logging.LogFactory;
021: import com.ibatis.sqlmap.engine.transaction.IsolationLevel;
022: import com.ibatis.sqlmap.engine.transaction.Transaction;
023: import com.ibatis.sqlmap.engine.transaction.TransactionException;
024:
025: import javax.sql.DataSource;
026: import java.sql.Connection;
027: import java.sql.SQLException;
028:
029: public class ExternalTransaction implements Transaction {
030:
031: private static final Log connectionLog = LogFactory
032: .getLog(Connection.class);
033:
034: private DataSource dataSource;
035: private boolean defaultAutoCommit;
036: private boolean setAutoCommitAllowed;
037: private Connection connection;
038: private IsolationLevel isolationLevel = new IsolationLevel();
039:
040: public ExternalTransaction(DataSource ds,
041: boolean defaultAutoCommit, boolean setAutoCommitAllowed,
042: int isolationLevel) throws TransactionException {
043: // Check Parameters
044: dataSource = ds;
045: if (dataSource == null) {
046: throw new TransactionException(
047: "ExternalTransaction initialization failed. DataSource was null.");
048: }
049:
050: this .defaultAutoCommit = defaultAutoCommit;
051: this .setAutoCommitAllowed = setAutoCommitAllowed;
052: this .isolationLevel.setIsolationLevel(isolationLevel);
053: }
054:
055: private void init() throws SQLException, TransactionException {
056: // Open JDBC Transaction
057: connection = dataSource.getConnection();
058: if (connection == null) {
059: throw new TransactionException(
060: "ExternalTransaction could not start transaction. Cause: The DataSource returned a null connection.");
061: }
062: // Isolation Level
063: isolationLevel.applyIsolationLevel(connection);
064: // AutoCommit
065: if (setAutoCommitAllowed) {
066: if (connection.getAutoCommit() != defaultAutoCommit) {
067: connection.setAutoCommit(defaultAutoCommit);
068: }
069: }
070: // Debug
071: if (connectionLog.isDebugEnabled()) {
072: connection = ConnectionLogProxy.newInstance(connection);
073: }
074: }
075:
076: public void commit() throws SQLException, TransactionException {
077: }
078:
079: public void rollback() throws SQLException, TransactionException {
080: }
081:
082: public void close() throws SQLException, TransactionException {
083: if (connection != null) {
084: try {
085: isolationLevel.restoreIsolationLevel(connection);
086: } finally {
087: connection.close();
088: connection = null;
089: }
090: }
091: }
092:
093: public Connection getConnection() throws SQLException,
094: TransactionException {
095: if (connection == null) {
096: init();
097: }
098: return connection;
099: }
100:
101: }
|