본문 바로가기
FullStack/21. Java

MyBatis에서 Connection 가져오기

by nakanara 2020. 7. 27.
반응형

MyBatis에서 Connection 가져오기

별도 Connection이 아닌 Mybatis와 함께 트랜잭션이 가능한 방법을 고려하기 위해서 Mybatis에서 Connection 추출

특정 Transaction에서 가져오기


public TransactionStatus begin(){
    TransactionStatus txStatus = transactionManager.getTransaction(new DefaultTransactionDefinition());
    return txStatus;
}

public void commit(TransactionStatus txStatus) {
    transactionManager.commit(txStatus);
}

public void rollback(TransactionStatus txStatus) {
    transactionManager.rollback(txStatus);
}


public Connection getConnection(TransactionStatus txStatus){

    JdbcTransactionObjectSupport jdbcTransactionObjectSupport = (JdbcTransactionObjectSupport)((DefaultTransactionStatus)txStatus).getTransaction();
    return jdbcTransactionObjectSupport.getConnectionHolder().getConnection();
}

해당 Transaction에서 가져오기

JdbcTransactionObjectSupport txImplementation = (JdbcTransactionObjectSupport) ((DefaultTransactionStatus) getCurrentTransactionStatus())
                .getTransaction();
        return txImplementation.getConnectionHolder().getConnectionHandle();
}

#mybatis #connection #transaction

반응형