diff --git a/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java index d4490fed75..5cd4462f24 100755 --- a/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java @@ -67,8 +67,11 @@ public class StatelessSessionImpl extends AbstractSharedSessionContract implemen private PersistenceContext temporaryPersistenceContext = new StatefulPersistenceContext( this ); + private boolean connectionProvided; + StatelessSessionImpl(SessionFactoryImpl factory, SessionCreationOptions options) { super( factory, options ); + connectionProvided = options.getConnection() != null; } @Override @@ -648,6 +651,11 @@ public class StatelessSessionImpl extends AbstractSharedSessionContract implemen } } + @Override + public boolean isTransactionInProgress() { + return connectionProvided || super.isTransactionInProgress(); + } + @Override public void flushBeforeTransactionCompletion() { boolean flush = false; diff --git a/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionTest.java b/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionTest.java index de5be97773..2ae3f73a2f 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionTest.java @@ -8,14 +8,17 @@ package org.hibernate.test.stateless; import java.util.Date; -import org.junit.Test; - import org.hibernate.ScrollMode; import org.hibernate.ScrollableResults; import org.hibernate.StatelessSession; import org.hibernate.Transaction; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.query.NativeQuery; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; @@ -38,14 +41,14 @@ public class StatelessSessionTest extends BaseCoreFunctionalTestCase { Date initVersion = doc.getLastModified(); assertNotNull( initVersion ); tx.commit(); - + tx = ss.beginTransaction(); doc.setText("blah blah blah .... blah"); ss.update(doc); assertNotNull( doc.getLastModified() ); assertNotSame( doc.getLastModified(), initVersion ); tx.commit(); - + tx = ss.beginTransaction(); doc.setText("blah blah blah .... blah blay"); ss.update(doc); @@ -166,5 +169,17 @@ public class StatelessSessionTest extends BaseCoreFunctionalTestCase { ss.close(); } + @Test + @TestForIssue( jiraKey = "HHH-12141" ) + public void testInsertInStatelessSession() throws Exception { + doInHibernate( this::sessionFactory, session -> { + session.doWork( connection -> { + StatelessSession sls = sessionFactory().openStatelessSession(connection); + NativeQuery q = sls.createNativeQuery( "INSERT INTO paper (color) values ('red')"); + q.executeUpdate(); + } ); + } ); + } + }