From 44763b4c28ff5b4c3dc90a9ab11db20d4cd001db Mon Sep 17 00:00:00 2001 From: Yannick Marcon Date: Thu, 25 Aug 2022 11:01:18 +0200 Subject: [PATCH] Spring hibernate mongodb (#432) * hibernate 5, spring 5, mongo driver 4 * migration to new mongodb java api to support all mongodb versions --- magma-api/pom.xml | 2 +- magma-beans/pom.xml | 2 +- magma-crypt/pom.xml | 2 +- magma-data-generator/pom.xml | 2 +- magma-datasource-csv/pom.xml | 2 +- magma-datasource-excel/pom.xml | 2 +- magma-datasource-fs/pom.xml | 2 +- magma-datasource-hibernate/pom.xml | 2 +- .../hibernate/HibernateDatasource.java | 12 +- .../hibernate/HibernateValueTableWriter.java | 5 +- .../hibernate/cfg/MagmaDialectResolver.java | 16 +- .../converter/VariableEntityConverter.java | 2 +- .../support/LocalSessionFactoryProvider.java | 19 +- .../hibernate/type/ValueHibernateType.java | 81 +++--- .../type/ValueTypeHibernateType.java | 31 +-- .../src/test/resources/test-context.xml | 12 +- magma-datasource-jdbc/pom.xml | 2 +- .../jdbc/JdbcVariableValueSource.java | 1 - .../datasource/jdbc/JdbcDatasourceTest.java | 4 +- .../test/SchemaTestExecutionListener.java | 3 +- .../test/resources/test-spring-context.xml | 5 + magma-datasource-mongodb/pom.xml | 4 +- .../datasource/mongodb/MongoDBDatasource.java | 88 +++---- .../mongodb/MongoDBDatasourceFactory.java | 18 +- .../datasource/mongodb/MongoDBFactory.java | 65 ++--- .../mongodb/MongoDBValueLoaderFactory.java | 21 +- .../datasource/mongodb/MongoDBValueSet.java | 63 ++--- .../mongodb/MongoDBValueSetBatch.java | 8 +- .../mongodb/MongoDBValueSetFetcher.java | 19 +- .../datasource/mongodb/MongoDBValueTable.java | 80 +++--- .../mongodb/MongoDBValueTableWriter.java | 241 ++++++++++-------- .../datasource/mongodb/MongoDBVariable.java | 17 +- .../MongoDBVariableEntityProvider.java | 24 +- .../mongodb/MongoDBVariableValueSource.java | 21 +- .../MongoDBVariableValueSourceFactory.java | 16 +- .../mongodb/converter/ValueConverter.java | 58 ++--- .../mongodb/converter/VariableConverter.java | 69 +++-- .../mongodb/MongoDBDatasourceTest.java | 2 + magma-datasource-null/pom.xml | 2 +- magma-filter/pom.xml | 2 +- magma-integration/pom.xml | 4 +- .../src/test/resources/test-context.xml | 12 +- magma-js/pom.xml | 2 +- magma-math/pom.xml | 2 +- magma-security/pom.xml | 2 +- magma-spring/pom.xml | 2 +- magma-test/pom.xml | 2 +- magma-xstream/pom.xml | 2 +- pom.xml | 12 +- 49 files changed, 534 insertions(+), 533 deletions(-) diff --git a/magma-api/pom.xml b/magma-api/pom.xml index 259635f52..8753c4330 100644 --- a/magma-api/pom.xml +++ b/magma-api/pom.xml @@ -17,7 +17,7 @@ org.obiba.magma magma - 3.1-SNAPSHOT + 4.0-SNAPSHOT ../pom.xml diff --git a/magma-beans/pom.xml b/magma-beans/pom.xml index 36ffe28b3..4fbd75675 100644 --- a/magma-beans/pom.xml +++ b/magma-beans/pom.xml @@ -17,7 +17,7 @@ org.obiba.magma magma - 3.1-SNAPSHOT + 4.0-SNAPSHOT ../pom.xml diff --git a/magma-crypt/pom.xml b/magma-crypt/pom.xml index 907e25ffb..decb9576f 100644 --- a/magma-crypt/pom.xml +++ b/magma-crypt/pom.xml @@ -17,7 +17,7 @@ org.obiba.magma magma - 3.1-SNAPSHOT + 4.0-SNAPSHOT ../pom.xml diff --git a/magma-data-generator/pom.xml b/magma-data-generator/pom.xml index 4d199620a..33741c5f3 100644 --- a/magma-data-generator/pom.xml +++ b/magma-data-generator/pom.xml @@ -17,7 +17,7 @@ org.obiba.magma magma - 3.1-SNAPSHOT + 4.0-SNAPSHOT ../pom.xml diff --git a/magma-datasource-csv/pom.xml b/magma-datasource-csv/pom.xml index f42d804ce..0da94efe5 100644 --- a/magma-datasource-csv/pom.xml +++ b/magma-datasource-csv/pom.xml @@ -17,7 +17,7 @@ org.obiba.magma magma - 3.1-SNAPSHOT + 4.0-SNAPSHOT ../pom.xml diff --git a/magma-datasource-excel/pom.xml b/magma-datasource-excel/pom.xml index 6efa2eb9b..ca5e8301f 100644 --- a/magma-datasource-excel/pom.xml +++ b/magma-datasource-excel/pom.xml @@ -17,7 +17,7 @@ org.obiba.magma magma - 3.1-SNAPSHOT + 4.0-SNAPSHOT ../pom.xml diff --git a/magma-datasource-fs/pom.xml b/magma-datasource-fs/pom.xml index 2d9fc741e..1c5b08637 100644 --- a/magma-datasource-fs/pom.xml +++ b/magma-datasource-fs/pom.xml @@ -17,7 +17,7 @@ org.obiba.magma magma - 3.1-SNAPSHOT + 4.0-SNAPSHOT ../pom.xml diff --git a/magma-datasource-hibernate/pom.xml b/magma-datasource-hibernate/pom.xml index 932571adf..2c86ce13c 100644 --- a/magma-datasource-hibernate/pom.xml +++ b/magma-datasource-hibernate/pom.xml @@ -17,7 +17,7 @@ org.obiba.magma magma - 3.1-SNAPSHOT + 4.0-SNAPSHOT ../pom.xml diff --git a/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/HibernateDatasource.java b/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/HibernateDatasource.java index cb37bd558..ed460e260 100644 --- a/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/HibernateDatasource.java +++ b/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/HibernateDatasource.java @@ -105,8 +105,10 @@ public ValueTableWriter createWriter(@NotNull String tableName, @NotNull String } else { // Create table transaction. ValueTableState valueTableState = new ValueTableState(tableName, entityType, getDatasourceState()); - getSessionFactory().getCurrentSession().save(valueTableState); - getSessionFactory().getCurrentSession().refresh(valueTableState); //OPAL-2635 + Session session = getSessionFactory().getCurrentSession(); + session.save(valueTableState); + session.flush(); + session.refresh(valueTableState); //OPAL-2635 // Create a transaction for a new table valueTableTransaction = newTableTransaction(new HibernateValueTable(this, valueTableState), true); } @@ -227,8 +229,10 @@ protected void onInitialise() { // If datasource not persisted, create the persisted DatasourceState. if(datasourceState == null) { datasourceState = new DatasourceState(getName()); - sessionFactory.getCurrentSession().save(datasourceState); - sessionFactory.getCurrentSession().refresh(datasourceState); //OPAL-2635 + Session session = sessionFactory.getCurrentSession(); + session.save(datasourceState); + session.flush(); + session.refresh(datasourceState); //OPAL-2635 } else { // If already persisted, load the persisted attributes for that datasource. for(AttributeState attribute : datasourceState.getAttributes()) { diff --git a/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/HibernateValueTableWriter.java b/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/HibernateValueTableWriter.java index 846d33c8e..64c255bb4 100644 --- a/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/HibernateValueTableWriter.java +++ b/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/HibernateValueTableWriter.java @@ -81,8 +81,8 @@ class HibernateValueTableWriter implements ValueTableWriter { session = valueTable.getDatasource().getSessionFactory().getCurrentSession(); valueSourceFactory = new HibernateVariableValueSourceFactory(valueTable); - if(session.getFlushMode() != FlushMode.MANUAL) { - session.setFlushMode(FlushMode.MANUAL); + if(session.getHibernateFlushMode() != FlushMode.MANUAL) { + session.setHibernateFlushMode(FlushMode.MANUAL); } context = valueTable.createContext(); @@ -261,6 +261,7 @@ private HibernateValueSetWriter(@NotNull VariableEntity entity) { state = new ValueSetState(valueTable.getValueTableState(), variableEntityState); // Persists the ValueSet session.save(state); + session.flush(); session.refresh(state); //OPAL-2635 values = Maps.newHashMap(); isNewValueSet = true; diff --git a/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/cfg/MagmaDialectResolver.java b/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/cfg/MagmaDialectResolver.java index c9d78b9a2..c51ff6909 100644 --- a/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/cfg/MagmaDialectResolver.java +++ b/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/cfg/MagmaDialectResolver.java @@ -9,11 +9,13 @@ */ package org.obiba.magma.datasource.hibernate.cfg; +import org.hibernate.dialect.Database; import org.hibernate.dialect.Dialect; import org.hibernate.dialect.HSQLDialect; import org.hibernate.dialect.MySQL5Dialect; import org.hibernate.engine.jdbc.dialect.internal.StandardDialectResolver; import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo; +import org.hibernate.engine.jdbc.dialect.spi.DialectResolver; /** * Ensures usage of InnoDB for MySQL databases and uses custom dialect for HSQLDB, otherwise, fallback to default @@ -21,17 +23,27 @@ *

* This class is instantiated by Hibernate itself through the {@code hibernate.properties} file. */ -public class MagmaDialectResolver extends StandardDialectResolver { +public class MagmaDialectResolver implements DialectResolver { private static final long serialVersionUID = 6167226108895659666L; @Override @SuppressWarnings("ChainOfInstanceofChecks") public Dialect resolveDialect(DialectResolutionInfo info) { - Dialect dialect = super.resolveDialect(info); + Dialect dialect = resolveStandardDialect(info); if(dialect instanceof MySQL5Dialect) return new MySQL5InnoDbUtf8Dialect(); if(dialect instanceof HSQLDialect) return new MagmaHSQLDialect(); return dialect; } + private Dialect resolveStandardDialect(DialectResolutionInfo info) { + for ( Database database : Database.values() ) { + Dialect dialect = database.resolveDialect( info ); + if ( dialect != null ) { + return dialect; + } + } + return null; + } + } diff --git a/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/converter/VariableEntityConverter.java b/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/converter/VariableEntityConverter.java index 8a338f504..fd32cf947 100644 --- a/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/converter/VariableEntityConverter.java +++ b/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/converter/VariableEntityConverter.java @@ -31,7 +31,7 @@ public VariableEntityState marshal(VariableEntity variableEntity, HibernateMarsh if(variableEntityState == null) { variableEntityState = new VariableEntityState(variableEntity.getIdentifier(), variableEntity.getType()); context.getSessionFactory().getCurrentSession().save(variableEntityState); - context.getSessionFactory().getCurrentSession().refresh(variableEntityState); //OPAL-2635 + //context.getSessionFactory().getCurrentSession().refresh(variableEntityState); //OPAL-2635 } return variableEntityState; diff --git a/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/support/LocalSessionFactoryProvider.java b/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/support/LocalSessionFactoryProvider.java index 27181aceb..5b111a28c 100644 --- a/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/support/LocalSessionFactoryProvider.java +++ b/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/support/LocalSessionFactoryProvider.java @@ -10,21 +10,21 @@ package org.obiba.magma.datasource.hibernate.support; -import java.util.Properties; - -import javax.annotation.Nullable; -import javax.sql.DataSource; -import javax.validation.constraints.NotNull; - import org.hibernate.SessionFactory; +import org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl; import org.hibernate.cfg.Environment; -import org.hibernate.cfg.ImprovedNamingStrategy; +import org.obiba.core.service.impl.hibernate.PhysicalNamingStrategyImpl; import org.obiba.magma.Initialisable; import org.obiba.magma.MagmaRuntimeException; import org.obiba.magma.datasource.hibernate.SessionFactoryProvider; import org.obiba.magma.datasource.hibernate.cfg.HibernateConfigurationHelper; import org.obiba.magma.datasource.hibernate.cfg.MagmaDialectResolver; -import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder; +import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder; + +import javax.annotation.Nullable; +import javax.sql.DataSource; +import javax.validation.constraints.NotNull; +import java.util.Properties; public class LocalSessionFactoryProvider implements SessionFactoryProvider, Initialisable { @@ -53,7 +53,8 @@ public void initialise() { // Set some reasonable defaults LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource); builder.addAnnotatedClasses(HibernateConfigurationHelper.getAnnotatedTypesAsArray()); - builder.setNamingStrategy(ImprovedNamingStrategy.INSTANCE); + builder.setImplicitNamingStrategy(ImplicitNamingStrategyLegacyHbmImpl.INSTANCE); + builder.setPhysicalNamingStrategy(PhysicalNamingStrategyImpl.INSTANCE); builder.setProperty(Environment.DIALECT, dialect); builder.setProperty(Environment.HBM2DDL_AUTO, "update"); builder.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true"); diff --git a/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/type/ValueHibernateType.java b/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/type/ValueHibernateType.java index 498c06ff5..04349818c 100644 --- a/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/type/ValueHibernateType.java +++ b/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/type/ValueHibernateType.java @@ -10,27 +10,24 @@ package org.obiba.magma.datasource.hibernate.type; -import java.io.StringReader; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Types; -import java.util.Map; - -import javax.annotation.Nullable; - -import org.dom4j.Node; +import com.google.common.base.Strings; import org.hibernate.HibernateException; import org.hibernate.MappingException; +import org.hibernate.engine.jdbc.Size; import org.hibernate.engine.spi.Mapping; import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.engine.spi.SessionImplementor; -import org.hibernate.metamodel.relational.Size; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.type.AbstractType; import org.obiba.magma.Value; import org.obiba.magma.ValueType; -import com.google.common.base.Strings; +import javax.annotation.Nullable; +import java.io.StringReader; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; +import java.util.Map; /** * A Hibernate Type for persisting {@code Value} instances. The strategy uses 3 columns: @@ -54,12 +51,6 @@ public String getName() { return "Value"; } - @Override - public boolean isDirty(Object old, Object current, boolean[] checkable, SessionImplementor session) - throws HibernateException { - return !old.equals(current); - } - @Override public boolean isMutable() { // Value instances are immutable @@ -67,15 +58,23 @@ public boolean isMutable() { } @Override - public Object nullSafeGet(ResultSet rs, String name, SessionImplementor session, Object owner) - throws HibernateException, SQLException { - throw new UnsupportedOperationException(); + public Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner, Map copyCache) throws HibernateException { + return null; + } + + @Override + public Class getReturnedClass() { + return Value.class; + } + + @Override + public boolean isDirty(Object oldState, Object currentState, boolean[] checkable, SharedSessionContractImplementor session) throws HibernateException { + return !oldState.equals(currentState); } @Nullable @Override - public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) - throws HibernateException, SQLException { + public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException { String valueTypeName = rs.getString(names[0]); // Even when the column is NOT NULL, a SELECT statement can return NULL (using a left join for example). // When this column is null, we cannot construct a valid {@code Value} instance, so this method returns null @@ -89,8 +88,12 @@ public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor sessi } @Override - public void nullSafeSet(PreparedStatement st, Object obj, int index, boolean[] settable, SessionImplementor session) - throws HibernateException, SQLException { + public Object nullSafeGet(ResultSet rs, String name, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void nullSafeSet(PreparedStatement st, Object obj, int index, boolean[] settable, SharedSessionContractImplementor session) throws HibernateException, SQLException { Value value = (Value) obj; int offset = 0; @@ -106,8 +109,7 @@ public void nullSafeSet(PreparedStatement st, Object obj, int index, boolean[] s } @Override - public void nullSafeSet(PreparedStatement st, Object obj, int index, SessionImplementor session) - throws HibernateException, SQLException { + public void nullSafeSet(PreparedStatement st, Object obj, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { Value value = (Value) obj; st.setString(index, value.getValueType().getName()); st.setBoolean(index + 1, value.isSequence()); @@ -115,29 +117,6 @@ public void nullSafeSet(PreparedStatement st, Object obj, int index, SessionImpl st.setClob(index + 2, new StringReader(stringValue), stringValue.length()); } - @Override - public Class getReturnedClass() { - return Value.class; - } - - @Override - @SuppressWarnings({ "unchecked", "rawtypes" }) - public Object replace(Object original, Object target, SessionImplementor session, Object owner, Map copyCache) - throws HibernateException { - // It is safe to return the original parameter since Value instances are immutable - return original; - } - - @Override - public Object fromXMLNode(Node xml, Mapping factory) throws HibernateException { - throw new UnsupportedOperationException(); - } - - @Override - public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) throws HibernateException { - throw new UnsupportedOperationException(); - } - @Override public int[] sqlTypes(Mapping mapping) throws MappingException { return new int[] { Types.VARCHAR, Types.BIT, Types.CLOB }; diff --git a/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/type/ValueTypeHibernateType.java b/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/type/ValueTypeHibernateType.java index 3b5a552e1..d78265535 100644 --- a/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/type/ValueTypeHibernateType.java +++ b/magma-datasource-hibernate/src/main/java/org/obiba/magma/datasource/hibernate/type/ValueTypeHibernateType.java @@ -10,16 +10,9 @@ package org.obiba.magma.datasource.hibernate.type; -import java.io.Reader; -import java.sql.Clob; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Types; - import org.hibernate.HibernateException; import org.hibernate.dialect.Dialect; -import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.type.AbstractStandardBasicType; import org.hibernate.type.DiscriminatorType; import org.hibernate.type.descriptor.WrapperOptions; @@ -28,6 +21,12 @@ import org.hibernate.type.descriptor.sql.VarcharTypeDescriptor; import org.obiba.magma.ValueType; +import java.io.Reader; +import java.sql.Clob; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Types; + /** * String representation of {@link ValueType}. */ @@ -50,17 +49,6 @@ public ValueType fromStringValue(String xml) throws HibernateException { return ValueType.Factory.forName(xml); } - @Override - public Object get(ResultSet rs, String name, SessionImplementor session) throws HibernateException, SQLException { - return ValueType.Factory.forName(rs.getString(name)); - } - - @Override - public void set(PreparedStatement st, ValueType value, int index, SessionImplementor session) - throws HibernateException, SQLException { - st.setString(index, toString(value)); - } - @Override public String toString(ValueType value) throws HibernateException { return value.getName(); @@ -72,7 +60,7 @@ public String getName() { } @Override - public void nullSafeSet(PreparedStatement st, Object value, int index, boolean[] settable, SessionImplementor session) + public void nullSafeSet(PreparedStatement st, Object value, int index, boolean[] settable, SharedSessionContractImplementor session) throws HibernateException, SQLException { if(value == null) { st.setNull(index, Types.VARCHAR); @@ -117,6 +105,9 @@ public X unwrap(ValueType value, Class type, WrapperOptions options) { if(ValueType.class.isAssignableFrom(type)) { return (X) value; } + if(String.class.isAssignableFrom(type)) { + return (X) value.getName(); + } throw unknownUnwrap(type); } diff --git a/magma-datasource-hibernate/src/test/resources/test-context.xml b/magma-datasource-hibernate/src/test/resources/test-context.xml index 07b64ad0b..89a443b42 100644 --- a/magma-datasource-hibernate/src/test/resources/test-context.xml +++ b/magma-datasource-hibernate/src/test/resources/test-context.xml @@ -25,11 +25,13 @@ - + + + + - - - + + @@ -42,7 +44,7 @@ - + diff --git a/magma-datasource-jdbc/pom.xml b/magma-datasource-jdbc/pom.xml index b73af32cb..ce1f6b05f 100644 --- a/magma-datasource-jdbc/pom.xml +++ b/magma-datasource-jdbc/pom.xml @@ -17,7 +17,7 @@ org.obiba.magma magma - 3.1-SNAPSHOT + 4.0-SNAPSHOT ../pom.xml diff --git a/magma-datasource-jdbc/src/main/java/org/obiba/magma/datasource/jdbc/JdbcVariableValueSource.java b/magma-datasource-jdbc/src/main/java/org/obiba/magma/datasource/jdbc/JdbcVariableValueSource.java index 11cb22945..173cc59c0 100644 --- a/magma-datasource-jdbc/src/main/java/org/obiba/magma/datasource/jdbc/JdbcVariableValueSource.java +++ b/magma-datasource-jdbc/src/main/java/org/obiba/magma/datasource/jdbc/JdbcVariableValueSource.java @@ -198,7 +198,6 @@ private ResultSet newCursor() throws SQLException { @Override public void remove() { - closeQuietly(); throw new UnsupportedOperationException(); } diff --git a/magma-datasource-jdbc/src/test/java/org/obiba/magma/datasource/jdbc/JdbcDatasourceTest.java b/magma-datasource-jdbc/src/test/java/org/obiba/magma/datasource/jdbc/JdbcDatasourceTest.java index c41d48ba7..a65f8428c 100644 --- a/magma-datasource-jdbc/src/test/java/org/obiba/magma/datasource/jdbc/JdbcDatasourceTest.java +++ b/magma-datasource-jdbc/src/test/java/org/obiba/magma/datasource/jdbc/JdbcDatasourceTest.java @@ -35,8 +35,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; import org.springframework.test.context.support.DirtiesContextTestExecutionListener; -import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.test.context.transaction.TransactionalTestExecutionListener; +import org.springframework.transaction.annotation.Transactional; import javax.sql.DataSource; import java.util.List; @@ -48,7 +48,7 @@ @SuppressWarnings({"ReuseOfLocalVariable", "OverlyLongMethod", "PMD.NcssMethodCount"}) @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/test-spring-context.xml") -@TransactionConfiguration +@Transactional @TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class, SchemaTestExecutionListener.class, DbUnitAwareTestExecutionListener.class}) diff --git a/magma-datasource-jdbc/src/test/java/org/obiba/magma/test/SchemaTestExecutionListener.java b/magma-datasource-jdbc/src/test/java/org/obiba/magma/test/SchemaTestExecutionListener.java index 80b5cf00f..df20da6fc 100644 --- a/magma-datasource-jdbc/src/test/java/org/obiba/magma/test/SchemaTestExecutionListener.java +++ b/magma-datasource-jdbc/src/test/java/org/obiba/magma/test/SchemaTestExecutionListener.java @@ -19,6 +19,7 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.init.ScriptUtils; import org.springframework.test.context.TestContext; import org.springframework.test.context.TestExecutionListener; import org.springframework.test.jdbc.JdbcTestUtils; @@ -72,7 +73,7 @@ private void handleAnnotation(TestContext testContext, TestSchema testSchemaAnno if(!schemaLocation.isEmpty()) { sqlScript = schemaLocation + "/" + sqlScript; } - JdbcTestUtils.executeSqlScript(new JdbcTemplate(dataSource), new ClassPathResource(sqlScript), true); + ScriptUtils.executeSqlScript(dataSource.getConnection(), new ClassPathResource(sqlScript)); } } } \ No newline at end of file diff --git a/magma-datasource-jdbc/src/test/resources/test-spring-context.xml b/magma-datasource-jdbc/src/test/resources/test-spring-context.xml index 7432af083..1f098d364 100644 --- a/magma-datasource-jdbc/src/test/resources/test-spring-context.xml +++ b/magma-datasource-jdbc/src/test/resources/test-spring-context.xml @@ -19,6 +19,11 @@ + + + + +