22
33import static org .junit .jupiter .api .Assumptions .assumeTrue ;
44
5- import java .net .URL ;
65import java .sql .Connection ;
76import java .sql .SQLException ;
87import java .util .ArrayList ;
9- import java .util .Collections ;
10- import java .util .HashMap ;
118import java .util .List ;
129import java .util .Map ;
1310
1411import javax .sql .DataSource ;
1512
16- import org .hibernate .cfg .AvailableSettings ;
17- import org .hibernate .jpa .boot .spi .Bootstrap ;
1813import org .junit .jupiter .params .ParameterizedTest ;
1914import org .junit .jupiter .params .provider .Arguments ;
2015import org .junit .jupiter .params .provider .MethodSource ;
2116import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
2217import org .springframework .core .env .ConfigurableEnvironment ;
2318import org .springframework .core .env .MapPropertySource ;
2419import org .springframework .core .env .MutablePropertySources ;
25- import org .springframework .jdbc .datasource .DataSourceTransactionManager ;
2620import org .springframework .jdbc .datasource .init .DatabasePopulator ;
21+ import org .springframework .orm .jpa .EntityManagerFactoryUtils ;
2722import org .springframework .transaction .PlatformTransactionManager ;
2823import org .springframework .transaction .TransactionDefinition ;
2924import org .springframework .transaction .support .DefaultTransactionDefinition ;
30- import org .springframework .transaction .support .TransactionOperations ;
3125import org .springframework .transaction .support .TransactionTemplate ;
3226
3327import com .github .marschall .hibernate .batchsequencegenerator .configurations .FirebirdConfiguration ;
3832import com .github .marschall .hibernate .batchsequencegenerator .configurations .OracleConfiguration ;
3933import com .github .marschall .hibernate .batchsequencegenerator .configurations .PostgresConfiguration ;
4034import com .github .marschall .hibernate .batchsequencegenerator .configurations .SqlServerConfiguration ;
35+ import com .github .marschall .hibernate .batchsequencegenerator .configurations .TransactionManagerConfiguration ;
4136import com .github .marschall .hibernate .batchsequencegenerator .entities .ChildEntity ;
4237import com .github .marschall .hibernate .batchsequencegenerator .entities .ParentEntity ;
4338
4439import jakarta .persistence .EntityManager ;
4540import jakarta .persistence .EntityManagerFactory ;
46- import jakarta .persistence .EntityTransaction ;
47- import jakarta .persistence .spi .PersistenceUnitTransactionType ;
4841
4942public class BatchSequenceGeneratorIntegrationTest {
5043
5144 private AnnotationConfigApplicationContext applicationContext ;
52- private EntityManagerFactory entityManagerFactory ;
45+ private TransactionTemplate template ;
5346
5447 public static List <Arguments > parameters () {
5548 List <Arguments > parameters = new ArrayList <>();
@@ -78,47 +71,21 @@ private void setUp(Class<?> dataSourceConfiguration, String persistenceUnitName)
7871 assumeTrue (isSupportedOnTravis (persistenceUnitName ));
7972 }
8073 this .applicationContext = new AnnotationConfigApplicationContext ();
81- this .applicationContext .register (dataSourceConfiguration );
74+ this .applicationContext .register (dataSourceConfiguration , HibernateConfiguration . class , TransactionManagerConfiguration . class );
8275 ConfigurableEnvironment environment = this .applicationContext .getEnvironment ();
8376 MutablePropertySources propertySources = environment .getPropertySources ();
84- Map <String , Object > source = Collections . singletonMap (HibernateConfiguration .PERSISTENCE_UNIT_NAME , persistenceUnitName );
77+ Map <String , Object > source = Map . of (HibernateConfiguration .PERSISTENCE_UNIT_NAME , persistenceUnitName );
8578 propertySources .addFirst (new MapPropertySource ("persistence unit name" , source ));
8679 this .applicationContext .refresh ();
8780
88- URL persistenceXml = BatchSequenceGeneratorIntegrationTest .class .getClassLoader ().getResource ("META-INF/persistence.xml" );
89- DataSource dataSource = this .applicationContext .getBean (DataSource .class );
90-
91- Map <String , Object > integrationSettings = new HashMap <>();
92- integrationSettings .put (AvailableSettings .DATASOURCE , dataSource );
93- this .entityManagerFactory = Bootstrap .getEntityManagerFactoryBuilder (persistenceXml , persistenceUnitName , PersistenceUnitTransactionType .RESOURCE_LOCAL , integrationSettings )
94- .withDataSource (dataSource )
95- .build ();
96-
97- this .populateDatabase (dataSource );
98- }
99-
100- private void populateDatabase (DataSource dataSource ) {
101- // PlatformTransactionManager txManager = this.applicationContext.getBean(PlatformTransactionManager.class);
102- PlatformTransactionManager txManager = new DataSourceTransactionManager (dataSource );
81+ PlatformTransactionManager txManager = this .applicationContext .getBean (PlatformTransactionManager .class );
10382 TransactionDefinition transactionDefinition = new DefaultTransactionDefinition (TransactionDefinition .PROPAGATION_REQUIRES_NEW );
104- TransactionOperations template = new TransactionTemplate (txManager , transactionDefinition );
105- template .execute (status -> {
106- return this .populateDatabaseInTransaction ();
83+ this . template = new TransactionTemplate (txManager , transactionDefinition );
84+ this . template .execute (status -> {
85+ return this .populateDatabase ();
10786 });
10887 }
10988
110- private Object populateDatabaseInTransaction () {
111- Map <String , DatabasePopulator > beans = this .applicationContext .getBeansOfType (DatabasePopulator .class );
112- DataSource dataSource = this .applicationContext .getBean (DataSource .class );
113- try (Connection connection = dataSource .getConnection ()) {
114- for (DatabasePopulator populator : beans .values ()) {
115- populator .populate (connection );
116- }
117- } catch (SQLException e ) {
118- throw new RuntimeException ("could initialize database" , e );
119- }
120- return null ;
121- }
12289
12390
12491 private static boolean isTravis () {
@@ -134,66 +101,57 @@ private static boolean isSupportedOnTravis(String persistenceUnitName) {
134101 || persistenceUnitName .contains ("db2" ));
135102 }
136103
104+ private Object populateDatabase () {
105+ Map <String , DatabasePopulator > beans = this .applicationContext .getBeansOfType (DatabasePopulator .class );
106+ DataSource dataSource = this .applicationContext .getBean (DataSource .class );
107+ try (Connection connection = dataSource .getConnection ()) {
108+ for (DatabasePopulator populator : beans .values ()) {
109+ populator .populate (connection );
110+ }
111+ } catch (SQLException e ) {
112+ throw new RuntimeException ("could initialize database" , e );
113+ }
114+ return null ;
115+ }
116+
137117 private void tearDown () {
138118 if (this .applicationContext == null ) { // unsupported database on travis
139119 return ;
140120 }
141- this .entityManagerFactory .close ();
142121 this .applicationContext .close ();
143122 }
144123
145124 @ ParameterizedTest
146125 @ MethodSource ("parameters" )
147126 public void parentChildInstert (Class <?> dataSourceConfiguration , String persistenceUnitName ) {
148127 this .setUp (dataSourceConfiguration , persistenceUnitName );
128+ EntityManagerFactory factory = this .applicationContext .getBean (EntityManagerFactory .class );
149129 try {
150- EntityManager entityManager = this .entityManagerFactory .createEntityManager ();
151- try {
152- this .runInTransaction (entityManager , () -> this .verifyParentChildInstert (entityManager ));
153- } finally {
154- entityManager .close ();
155- }
130+ this .template .execute (status -> {
131+ EntityManager entityManager = EntityManagerFactoryUtils .getTransactionalEntityManager (factory );
132+ int parentCount = 100 ;
133+ List <ParentEntity > parents = new ArrayList <>(parentCount );
134+ for (int i = 0 ; i < parentCount ; i ++) {
135+ ParentEntity parent = new ParentEntity ();
136+
137+ parent .addChild (new ChildEntity ());
138+ parent .addChild (new ChildEntity ());
139+
140+ parents .add (parent );
141+ }
142+ for (ParentEntity parent : parents ) {
143+ entityManager .persist (parent );
144+ for (ChildEntity child : parent .getChildren ()) {
145+ child .setParentId (parent .getParentId ());
146+ entityManager .persist (child );
147+ }
148+ }
149+ status .flush ();
150+ return null ;
151+ });
156152 } finally {
157153 this .tearDown ();
158154 }
159155 }
160156
161- private void runInTransaction (EntityManager entityManager , Runnable callback ) {
162- EntityTransaction transaction = entityManager .getTransaction ();
163- transaction .begin ();
164- try {
165- callback .run ();
166- if (transaction .getRollbackOnly ()) {
167- transaction .rollback ();
168- } else {
169- transaction .commit ();
170- }
171- } catch (Throwable t ) {
172- if (transaction .isActive ()) {
173- transaction .rollback ();
174- }
175- throw t ;
176- }
177- }
178-
179- private void verifyParentChildInstert (EntityManager entityManager ) {
180- int parentCount = 100 ;
181- List <ParentEntity > parents = new ArrayList <>(parentCount );
182- for (int i = 0 ; i < parentCount ; i ++) {
183- ParentEntity parent = new ParentEntity ();
184-
185- parent .addChild (new ChildEntity ());
186- parent .addChild (new ChildEntity ());
187-
188- parents .add (parent );
189- }
190- for (ParentEntity parent : parents ) {
191- entityManager .persist (parent );
192- for (ChildEntity child : parent .getChildren ()) {
193- child .setParentId (parent .getParentId ());
194- entityManager .persist (child );
195- }
196- }
197- }
198-
199157}
0 commit comments