All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dream.boot.transaction.SpringTransaction Maven / Gradle / Ivy

There is a newer version: 1.3.5
Show newest version
package com.dream.boot.transaction;

import com.dream.system.transaction.Transaction;
import org.springframework.jdbc.datasource.DataSourceUtils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class SpringTransaction implements Transaction {

    private DataSource dataSource;
    private Connection connection;
    private boolean isConnectionTransactional;
    private boolean autoCommit;

    public SpringTransaction(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public Connection getConnection() throws SQLException {
        if (connection == null) {
            openConnection();
        }
        return connection;
    }

    @Override
    public void commit() {
        if (this.connection != null && !this.isConnectionTransactional && !this.autoCommit) {
            try {
                this.connection.commit();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void rollback() {
        if (this.connection != null && !this.isConnectionTransactional && !this.autoCommit) {
            try {
                this.connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void close() {
        DataSourceUtils.releaseConnection(this.connection, this.dataSource);
    }

    @Override
    public boolean isAutoCommit() {
        return autoCommit;
    }

    @Override
    public void setAutoCommit(boolean autoCommit) {

    }

    protected void openConnection() throws SQLException {
        this.connection = DataSourceUtils.getConnection(this.dataSource);
        this.autoCommit = this.connection.getAutoCommit();
        this.isConnectionTransactional = DataSourceUtils.isConnectionTransactional(this.connection, this.dataSource);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy