From 975de86017d74693fdbfbe8459d914e56cbf5402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20Sch=C3=B6ne?= Date: Thu, 3 Dec 2015 12:48:41 +0100 Subject: [PATCH] HHH-10353 Interceptor.afterTransactionBegin() is not fired (cherry picked from commit fe488976928eae3753c7fcc7a8231001d28054d0) --- ...sourceLocalTransactionCoordinatorImpl.java | 1 + .../InterceptorTransactionEventTest.java | 80 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorTransactionEventTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jdbc/internal/JdbcResourceLocalTransactionCoordinatorImpl.java b/hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jdbc/internal/JdbcResourceLocalTransactionCoordinatorImpl.java index 2518848d62..dd1d5f962a 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jdbc/internal/JdbcResourceLocalTransactionCoordinatorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jdbc/internal/JdbcResourceLocalTransactionCoordinatorImpl.java @@ -134,6 +134,7 @@ public class JdbcResourceLocalTransactionCoordinatorImpl implements TransactionC if(this.timeOut > 0) { transactionCoordinatorOwner.setTransactionTimeOut( this.timeOut ); } + transactionCoordinatorOwner.afterTransactionBegin(); for ( TransactionObserver observer : observers ) { observer.afterBegin(); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorTransactionEventTest.java b/hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorTransactionEventTest.java new file mode 100644 index 0000000000..ee648088fb --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorTransactionEventTest.java @@ -0,0 +1,80 @@ +/* + * Copyright 2014 JBoss Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.hibernate.test.interceptor; + +import org.hibernate.EmptyInterceptor; +import org.hibernate.Interceptor; +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +/** + * Tests, whether {@link Interceptor} gets the transaction events + */ +public class InterceptorTransactionEventTest extends BaseCoreFunctionalTestCase { + + @Test + public void testTransactionEvents() throws Exception { + LoggingInterceptor interceptor = new LoggingInterceptor(); + + Session s = openSession(interceptor); + Transaction tx = s.beginTransaction(); + // Do nothing, open and closing the transaction is enough + tx.commit(); + s.close(); + + assertTrue("afterTransactionBeginCalled not called", interceptor.isAfterTransactionBeginCalled()); + assertTrue("afterTransactionCompletionCalled not called", interceptor.isAfterTransactionCompletionCalled()); + assertTrue("beforeTransactionCompletionCalled not called", interceptor.isBeforeTransactionCompletionCalled()); + } + + private static class LoggingInterceptor extends EmptyInterceptor { + private boolean afterTransactionBeginCalled; + private boolean afterTransactionCompletionCalled; + private boolean beforeTransactionCompletionCalled; + + @Override + public void afterTransactionBegin(Transaction tx) { + afterTransactionBeginCalled = true; + } + + @Override + public void afterTransactionCompletion(Transaction tx) { + afterTransactionCompletionCalled = true; + } + + @Override + public void beforeTransactionCompletion(Transaction tx) { + beforeTransactionCompletionCalled = true; + } + + public boolean isAfterTransactionBeginCalled() { + return afterTransactionBeginCalled; + } + + public boolean isAfterTransactionCompletionCalled() { + return afterTransactionCompletionCalled; + } + + public boolean isBeforeTransactionCompletionCalled() { + return beforeTransactionCompletionCalled; + } + + } +}