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

com.ecfront.easybi.dbutils.inner.Transaction Maven / Gradle / Ivy

There is a newer version: 2.8.0
Show newest version
package com.ecfront.easybi.dbutils.inner;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;

public class Transaction {


    public static ConnectionWrap open(String dsCode) {
        ConnectionWrap cw = threadLocalConnection.get();
        try {
            if (null == cw) {
                cw = DSLoader.getConnection(dsCode);
                threadLocalConnection.set(cw);
            }
            cw.conn.setAutoCommit(false);
        } catch (SQLException e) {
            logger.error("Connection open error.", e);
        }
        return cw;
    }

    public static void commit() {
        ConnectionWrap cw = threadLocalConnection.get();
        if (null != cw.conn) {
            try {
                cw.conn.commit();
            } catch (SQLException e) {
                logger.error("Connection commit error.", e);
            }
        }
        close();
    }

    public static void rollback() {
        ConnectionWrap cw = threadLocalConnection.get();
        if (null != cw.conn) {
            try {
                cw.conn.rollback();
            } catch (SQLException e) {
                logger.error("Connection rollback error.", e);
            }
        }
        close();
    }

    private static void close() {
        ConnectionWrap cw = threadLocalConnection.get();
        if (null != cw.conn) {
            try {
                cw.conn.close();
                threadLocalConnection.set(null);
            } catch (SQLException e) {
                logger.error("Connection close error.", e);
            }
        }
    }

    private static final ThreadLocal threadLocalConnection = new ThreadLocal();

    private static final Logger logger = LoggerFactory.getLogger(Transaction.class);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy