Skip to content

Commit 596c7eb

Browse files
committed
Fix TransactionRequiredException failures.
1 parent aab7513 commit 596c7eb

File tree

3 files changed

+111
-92
lines changed

3 files changed

+111
-92
lines changed

hibernate-core/src/test/java/org/hibernate/test/connections/ConnectionManagementTestCase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ public final void testManualDisconnectChain() throws Throwable {
234234
public final void testManualDisconnectWithOpenResources() throws Throwable {
235235
prepare();
236236
Session sessionUnderTest = getSessionUnderTest();
237+
sessionUnderTest.beginTransaction();
237238

238239
Silly silly = new Silly( "tester" );
239240
sessionUnderTest.save( silly );
@@ -256,6 +257,8 @@ public final void testManualDisconnectWithOpenResources() throws Throwable {
256257
sessionUnderTest.delete( silly );
257258
sessionUnderTest.flush();
258259

260+
sessionUnderTest.getTransaction().commit();
261+
259262
release( sessionUnderTest );
260263
done();
261264
}

hibernate-core/src/test/java/org/hibernate/test/generatedkeys/identity/IdentityGeneratedKeysTest.java

Lines changed: 97 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
*/
77
package org.hibernate.test.generatedkeys.identity;
88

9+
import javax.persistence.PersistenceException;
10+
import javax.persistence.TransactionRequiredException;
11+
912
import org.junit.Test;
1013

1114
import org.hibernate.Session;
@@ -15,9 +18,11 @@
1518
import org.hibernate.testing.RequiresDialectFeature;
1619
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
1720

21+
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
1822
import static org.junit.Assert.assertEquals;
1923
import static org.junit.Assert.assertNotNull;
2024
import static org.junit.Assert.assertNull;
25+
import static org.junit.Assert.fail;
2126

2227
/**
2328
* @author Steve Ebersole
@@ -49,119 +54,123 @@ public void testIdentityColumnGeneratedIds() {
4954
@Test
5055
public void testPersistOutsideTransaction() {
5156
Session s = openSession();
52-
53-
// first test save() which should force an immediate insert...
54-
MyEntity myEntity1 = new MyEntity( "test-save" );
55-
Long id = ( Long ) s.save( myEntity1 );
56-
assertNotNull( "identity column did not force immediate insert", id );
57-
assertEquals( id, myEntity1.getId() );
58-
59-
// next test persist() which should cause a delayed insert...
60-
long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
61-
MyEntity myEntity2 = new MyEntity( "test-persist");
62-
s.persist( myEntity2 );
63-
assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() );
64-
assertNull( myEntity2.getId() );
65-
66-
// an explicit flush should cause execution of the delayed insertion
67-
s.flush();
68-
assertEquals( "delayed persist insert not executed on flush", initialInsertCount + 1, sessionFactory().getStatistics().getEntityInsertCount() );
69-
s.close();
70-
71-
s = openSession();
72-
s.beginTransaction();
73-
s.delete( myEntity1 );
74-
s.delete( myEntity2 );
75-
s.getTransaction().commit();
76-
s.close();
57+
try {
58+
// first test save() which should force an immediate insert...
59+
MyEntity myEntity1 = new MyEntity( "test-save" );
60+
Long id = (Long) s.save( myEntity1 );
61+
assertNotNull( "identity column did not force immediate insert", id );
62+
assertEquals( id, myEntity1.getId() );
63+
64+
// next test persist() which should cause a delayed insert...
65+
long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
66+
MyEntity myEntity2 = new MyEntity( "test-persist" );
67+
s.persist( myEntity2 );
68+
assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() );
69+
assertNull( myEntity2.getId() );
70+
71+
// an explicit flush should cause execution of the delayed insertion
72+
s.flush();
73+
fail( "TransactionRequiredException required upon flush" );
74+
}
75+
catch ( PersistenceException ex ) {
76+
// expected
77+
assertTyping( TransactionRequiredException.class, ex );
78+
}
79+
finally {
80+
s.close();
81+
}
7782
}
7883

7984
@Test
8085
@SuppressWarnings( {"unchecked"})
8186
public void testPersistOutsideTransactionCascadedToNonInverseCollection() {
8287
long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
8388
Session s = openSession();
84-
MyEntity myEntity = new MyEntity( "test-persist");
85-
myEntity.getNonInverseChildren().add( new MyChild( "test-child-persist-non-inverse" ) );
86-
s.persist( myEntity );
87-
assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() );
88-
assertNull( myEntity.getId() );
89-
s.flush();
90-
assertEquals( "delayed persist insert not executed on flush", initialInsertCount + 2, sessionFactory().getStatistics().getEntityInsertCount() );
91-
s.close();
92-
93-
s = openSession();
94-
s.beginTransaction();
95-
s.createQuery( "delete MyChild" ).executeUpdate();
96-
s.createQuery( "delete MyEntity" ).executeUpdate();
97-
s.getTransaction().commit();
98-
s.close();
89+
try {
90+
MyEntity myEntity = new MyEntity( "test-persist" );
91+
myEntity.getNonInverseChildren().add( new MyChild( "test-child-persist-non-inverse" ) );
92+
s.persist( myEntity );
93+
assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() );
94+
assertNull( myEntity.getId() );
95+
s.flush();
96+
fail( "TransactionRequiredException required upon flush" );
97+
}
98+
catch ( PersistenceException ex ) {
99+
// expected
100+
assertTyping( TransactionRequiredException.class, ex );
101+
}
102+
finally {
103+
s.close();
104+
}
99105
}
100106

101107
@Test
102108
@SuppressWarnings( {"unchecked"})
103109
public void testPersistOutsideTransactionCascadedToInverseCollection() {
104110
long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
105111
Session s = openSession();
106-
MyEntity myEntity2 = new MyEntity( "test-persist-2");
107-
MyChild child = new MyChild( "test-child-persist-inverse" );
108-
myEntity2.getInverseChildren().add( child );
109-
child.setInverseParent( myEntity2 );
110-
s.persist( myEntity2 );
111-
assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() );
112-
assertNull( myEntity2.getId() );
113-
s.flush();
114-
assertEquals( "delayed persist insert not executed on flush", initialInsertCount + 2, sessionFactory().getStatistics().getEntityInsertCount() );
115-
s.close();
116-
117-
s = openSession();
118-
s.beginTransaction();
119-
s.createQuery( "delete MyChild" ).executeUpdate();
120-
s.createQuery( "delete MyEntity" ).executeUpdate();
121-
s.getTransaction().commit();
122-
s.close();
112+
try {
113+
MyEntity myEntity2 = new MyEntity( "test-persist-2" );
114+
MyChild child = new MyChild( "test-child-persist-inverse" );
115+
myEntity2.getInverseChildren().add( child );
116+
child.setInverseParent( myEntity2 );
117+
s.persist( myEntity2 );
118+
assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() );
119+
assertNull( myEntity2.getId() );
120+
s.flush();
121+
fail( "TransactionRequiredException expected upon flush." );
122+
}
123+
catch ( PersistenceException ex ) {
124+
// expected
125+
assertTyping( TransactionRequiredException.class, ex );
126+
}
127+
finally {
128+
s.close();
129+
}
123130
}
124131

125132
@Test
126133
public void testPersistOutsideTransactionCascadedToManyToOne() {
127134
long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
128135
Session s = openSession();
129-
MyEntity myEntity = new MyEntity( "test-persist");
130-
myEntity.setSibling( new MySibling( "test-persist-sibling-out" ) );
131-
s.persist( myEntity );
132-
assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() );
133-
assertNull( myEntity.getId() );
134-
s.flush();
135-
assertEquals( "delayed persist insert not executed on flush", initialInsertCount + 2, sessionFactory().getStatistics().getEntityInsertCount() );
136-
s.close();
137-
138-
s = openSession();
139-
s.beginTransaction();
140-
s.createQuery( "delete MyEntity" ).executeUpdate();
141-
s.createQuery( "delete MySibling" ).executeUpdate();
142-
s.getTransaction().commit();
143-
s.close();
136+
try {
137+
MyEntity myEntity = new MyEntity( "test-persist" );
138+
myEntity.setSibling( new MySibling( "test-persist-sibling-out" ) );
139+
s.persist( myEntity );
140+
assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() );
141+
assertNull( myEntity.getId() );
142+
s.flush();
143+
fail( "TransactionRequiredException expected upon flush." );
144+
}
145+
catch ( PersistenceException ex ) {
146+
// expected
147+
assertTyping( TransactionRequiredException.class, ex );
148+
}
149+
finally {
150+
s.close();
151+
}
144152
}
145153

146154
@Test
147155
public void testPersistOutsideTransactionCascadedFromManyToOne() {
148156
long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
149157
Session s = openSession();
150-
MyEntity myEntity2 = new MyEntity( "test-persist-2");
151-
MySibling sibling = new MySibling( "test-persist-sibling-in" );
152-
sibling.setEntity( myEntity2 );
153-
s.persist( sibling );
154-
assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() );
155-
assertNull( myEntity2.getId() );
156-
s.flush();
157-
assertEquals( "delayed persist insert not executed on flush", initialInsertCount + 2, sessionFactory().getStatistics().getEntityInsertCount() );
158-
s.close();
159-
160-
s = openSession();
161-
s.beginTransaction();
162-
s.createQuery( "delete MySibling" ).executeUpdate();
163-
s.createQuery( "delete MyEntity" ).executeUpdate();
164-
s.getTransaction().commit();
165-
s.close();
158+
try {
159+
MyEntity myEntity2 = new MyEntity( "test-persist-2" );
160+
MySibling sibling = new MySibling( "test-persist-sibling-in" );
161+
sibling.setEntity( myEntity2 );
162+
s.persist( sibling );
163+
assertEquals( "persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount() );
164+
assertNull( myEntity2.getId() );
165+
s.flush();
166+
fail( "TransactionRequiredException expected upon flush." );
167+
}
168+
catch ( PersistenceException ex ) {
169+
// expected
170+
assertTyping( TransactionRequiredException.class, ex );
171+
}
172+
finally {
173+
s.close();
174+
}
166175
}
167176
}

hibernate-core/src/test/java/org/hibernate/test/legacy/MasterDetailTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -969,24 +969,32 @@ public void execute(Connection connection) throws SQLException {
969969
@Test
970970
public void testCustomPersister() throws Exception {
971971
Session s = openSession();
972-
Custom c = new Custom();
972+
Custom c = c = new Custom();
973973
c.setName( "foo" );
974974
c.id="100";
975-
String id = (String) s.save(c);
976-
assertTrue( c==s.load(Custom.class, id) );
975+
s.beginTransaction();
976+
String id = id = (String) s.save( c );
977+
assertTrue( c == s.load( Custom.class, id ) );
977978
s.flush();
979+
s.getTransaction().commit();
978980
s.close();
981+
979982
s = openSession();
983+
s.beginTransaction();
980984
c = (Custom) s.load(Custom.class, id);
981985
assertTrue( c.getName().equals("foo") );
982986
c.setName( "bar" );
983987
s.flush();
988+
s.getTransaction().commit();
984989
s.close();
990+
985991
s = openSession();
992+
s.beginTransaction();
986993
c = (Custom) s.load(Custom.class, id);
987994
assertTrue( c.getName().equals("bar") );
988995
s.delete(c);
989996
s.flush();
997+
s.getTransaction().commit();
990998
s.close();
991999
s = openSession();
9921000
boolean none = false;
@@ -998,7 +1006,6 @@ public void testCustomPersister() throws Exception {
9981006
}
9991007
assertTrue(none);
10001008
s.close();
1001-
10021009
}
10031010

10041011
@Test

0 commit comments

Comments
 (0)