HHH-12141 - SQL insert in stateless session causes javax.persistence.TransactionRequiredException

This commit is contained in:
Vlad Mihalcea 2018-01-18 12:17:01 +02:00
parent d2866620f2
commit 0ba1c4a2f3
2 changed files with 28 additions and 5 deletions

View File

@ -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;

View File

@ -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();
} );
} );
}
}