Skip to content

Commit 1241d35

Browse files
committed
HHH-13307 : Added test
1 parent cec7329 commit 1241d35

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.connections;
8+
9+
import java.util.Map;
10+
11+
import javax.persistence.Entity;
12+
import javax.persistence.GeneratedValue;
13+
import javax.persistence.GenerationType;
14+
import javax.persistence.Id;
15+
16+
import org.hibernate.Session;
17+
import org.hibernate.cfg.AvailableSettings;
18+
import org.hibernate.cfg.Environment;
19+
import org.hibernate.dialect.H2Dialect;
20+
import org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl;
21+
import org.hibernate.internal.CoreMessageLogger;
22+
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
23+
import org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl;
24+
25+
import org.hibernate.testing.RequiresDialect;
26+
import org.hibernate.testing.TestForIssue;
27+
import org.hibernate.testing.jta.TestingJtaBootstrap;
28+
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
29+
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
30+
import org.hibernate.testing.logger.LoggerInspectionRule;
31+
import org.hibernate.testing.logger.Triggerable;
32+
import org.junit.Rule;
33+
import org.junit.Test;
34+
35+
import org.jboss.logging.Logger;
36+
37+
import static org.junit.Assert.assertFalse;
38+
39+
@TestForIssue( jiraKey = "HHH-13307" )
40+
@RequiresDialect(H2Dialect.class)
41+
public class JdbcBatchingAgressiveReleaseTest extends BaseNonConfigCoreFunctionalTestCase {
42+
43+
@Rule
44+
public LoggerInspectionRule logInspection = new LoggerInspectionRule(
45+
Logger.getMessageLogger( CoreMessageLogger.class, AbstractBatchImpl.class.getName() )
46+
);
47+
48+
private Triggerable triggerable = logInspection.watchForLogMessages( "HHH000010" );
49+
50+
51+
@Override
52+
@SuppressWarnings("unchecked")
53+
protected void addSettings(Map settings) {
54+
super.addSettings( settings );
55+
56+
TestingJtaBootstrap.prepare( settings );
57+
settings.put( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, JtaTransactionCoordinatorBuilderImpl.class.getName() );
58+
settings.put( Environment.CONNECTION_HANDLING, PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT.toString() );
59+
settings.put( Environment.GENERATE_STATISTICS, "true" );
60+
settings.put( Environment.STATEMENT_BATCH_SIZE, "500" );
61+
}
62+
63+
@Test
64+
public void testJdbcBatching() throws Throwable {
65+
triggerable.reset();
66+
67+
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
68+
Session session = openSession();
69+
// The following 2 entity inserts will be batched.
70+
session.persist( new Person( 1, "Jane" ) );
71+
session.persist( new Person( 2, "Sally" ) );
72+
// The following entity has an IDENTITY ID, which cannot be batched.
73+
// As a result the existing batch is forced to execute before the Thing can be
74+
// inserted.
75+
session.persist( new Thing( "it" ) );
76+
session.close();
77+
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
78+
79+
assertFalse( triggerable.wasTriggered() );
80+
}
81+
82+
@Override
83+
protected Class[] getAnnotatedClasses() {
84+
return new Class[] { Person.class, Thing.class };
85+
}
86+
87+
@Entity( name = "Person")
88+
public static class Person {
89+
90+
@Id
91+
private int id;
92+
private String name;
93+
94+
public Person() {
95+
}
96+
97+
public Person(int id, String name) {
98+
this.id = id;
99+
this.name = name;
100+
}
101+
}
102+
103+
@Entity( name = "Thing")
104+
public static class Thing {
105+
106+
@Id
107+
@GeneratedValue(strategy = GenerationType.IDENTITY)
108+
private int id;
109+
private String name;
110+
111+
public Thing() {
112+
}
113+
114+
public Thing(String name) {
115+
this.id = id;
116+
this.name = name;
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)