org.snaker.engine.access.mybatis.MybatisTransaction.java Source code

Java tutorial

Introduction

Here is the source code for org.snaker.engine.access.mybatis.MybatisTransaction.java

Source

/* Copyright 2012-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.snaker.engine.access.mybatis;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.transaction.Transaction;
import org.snaker.engine.access.transaction.TransactionObjectHolder;
import org.snaker.engine.helper.AssertHelper;

/**
 * mybatis??connection?? ?commit?rooback?
 * 
 * @author yuqs
 * @version 1.0
 */
public class MybatisTransaction implements Transaction {
    private static final Log log = LogFactory.getLog(MybatisTransaction.class);
    private DataSource dataSource;
    protected Connection connection;
    protected boolean autoCommit;

    public MybatisTransaction(DataSource dataSource) {
        AssertHelper.notNull(dataSource, "No DataSource specified");
        this.dataSource = dataSource;
    }

    @Override
    public Connection getConnection() throws SQLException {
        this.connection = (Connection) TransactionObjectHolder.get();
        if (this.connection == null) {
            this.connection = dataSource.getConnection();
        }
        this.autoCommit = this.connection.getAutoCommit();
        return this.connection;
    }

    @Override
    public void commit() throws SQLException {
        if (this.connection != null && !this.autoCommit && !isConnectionTransactional()) {
            if (log.isDebugEnabled()) {
                log.debug("Committing JDBC Connection [" + this.connection.hashCode() + "]");
            }
            this.connection.commit();
        }
    }

    @Override
    public void rollback() throws SQLException {
        if (this.connection != null && !this.autoCommit && !isConnectionTransactional()) {
            if (log.isDebugEnabled()) {
                log.debug("Rolling back JDBC Connection [" + this.connection.hashCode() + "]");
            }
            this.connection.rollback();
        }
    }

    private boolean isConnectionTransactional() {
        Connection holdCon = (Connection) TransactionObjectHolder.get();
        return (holdCon == connection || holdCon.equals(connection));
    }

    @Override
    public void close() throws SQLException {
        // not needed in this version
    }
}