Search

setAutoCommit=false;

spring: jpa: properties: hibernate: connection: provider_disables_autocommit: true datasource: hikari: auto-commit: false
YAML
복사
default 는 위와 반대이다.
위의 설정을 확인하는 곳은
LogicalConnectionManagedImpl 의 begin() 인데,
initiallyAutoCommit = !doConnectionsFromProviderHaveAutoCommitDisabled() && determineInitialAutoCommitMode(getConnectionForTransactionManagement());
YAML
복사
!doConnectionsFromProviderHaveAutoCommitDisabled() 이 부분이다.
위 설정을 해주지 않으면 provider_disables_autocommit=false 가 되고 ! 연산자로 인해 true 가 된다.
그러면 getConnectionForTransactionManagement() 메서드를 실행시키고 connection 을 hikari pool 에서 받아온다.
이렇게 되면 현재 시점에서 service method 를 실행시키지도 않았는데 transaction interceptor 에 의해 transaction 을 열면서 connection 까지 물게 된다.
그래서 위의 설정을 true 로 해주면, doConnectionsFromProviderHaveAutoCommitDisabled() 은 true 가 되고 ! 연산자에 의해 false 가 되면서 getConnectionForTransactionManagement() 는 실행시키지 않아 connection 을 이 시점에서 가져오지 않는다.

그러면 이 경우 connection 을 가져가는 시점은?

모든 준비를 마치고 StatementPreparerImpl 에서 connection() 을 호출할 때
logicalConnection().getPhysicalConnection() 한다. 이 때 acquireConnectionIfNeeded() 이 호출되고 이 때 physical connection 이 null 이기때문에 물리적 커넥션을 획득한다.