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

com.github.lontime.extspring.impl.copper.SpringTransaction Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.github.lontime.extspring.impl.copper;

import java.sql.Connection;

import javax.sql.DataSource;

import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

/**
 * SpringTransaction.
 * @author lontime
 * @since 1.0
 */
public abstract class SpringTransaction {

    protected abstract void execute(Connection con) throws Exception;

    public void run(PlatformTransactionManager transactionManager, DataSource dataSource, TransactionDefinition def) throws Exception {
        TransactionStatus txnStatus = transactionManager.getTransaction(def);
        try {
            Connection con = DataSourceUtils.getConnection(dataSource);
            try {
                execute(con);
            } finally {
                DataSourceUtils.releaseConnection(con, dataSource);
            }
        } catch (Exception e) {
            transactionManager.rollback(txnStatus);
            throw e;
        }
        transactionManager.commit(txnStatus);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy