HHH-6020: properly passing the JTA transaction setting to EJB3Configuration

This commit is contained in:
adamw 2011-04-29 16:16:39 +02:00
parent 997dd00880
commit 5f607c6b9f
4 changed files with 49 additions and 17 deletions

View File

@ -28,6 +28,7 @@ import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import org.hibernate.envers.event.EnversIntegrator;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.internal.BasicServiceRegistryImpl;
import org.hibernate.testing.AfterClassOnce;
@ -52,6 +53,8 @@ public abstract class AbstractEntityTest extends AbstractEnversTest {
public abstract void configure(Ejb3Configuration cfg);
public void addConfigurationProperties(Properties configuration) { }
private void closeEntityManager() {
if (entityManager != null) {
entityManager.close();
@ -78,33 +81,43 @@ public abstract class AbstractEntityTest extends AbstractEnversTest {
protected void init(boolean audited, String auditStrategy) throws IOException {
this.audited = audited;
cfg = new Ejb3Configuration();
Properties configValues = cfg.getProperties();
Properties configurationProperties = new Properties();
if (!audited) {
configValues.setProperty(EnversIntegrator.AUTO_REGISTER, "false");
configurationProperties.setProperty(EnversIntegrator.AUTO_REGISTER, "false");
}
configValues.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
configValues.setProperty(Environment.DIALECT, "org.hibernate.dialect.H2Dialect");
configValues.setProperty(Environment.DRIVER, "org.h2.Driver");
configValues.setProperty(Environment.USER, "sa");
configurationProperties.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
configurationProperties.setProperty(Environment.DIALECT, "org.hibernate.dialect.H2Dialect");
configurationProperties.setProperty(Environment.DRIVER, "org.h2.Driver");
configurationProperties.setProperty(Environment.USER, "sa");
// Separate database for each test class
configValues.setProperty(Environment.URL, "jdbc:h2:mem:" + this.getClass().getName() + ";DB_CLOSE_DELAY=-1");
configurationProperties.setProperty(Environment.URL, "jdbc:h2:mem:" + this.getClass().getName() + ";DB_CLOSE_DELAY=-1");
if (auditStrategy != null && !"".equals(auditStrategy)) {
cfg.setProperty("org.hibernate.envers.audit_strategy", auditStrategy);
configurationProperties.setProperty("org.hibernate.envers.audit_strategy", auditStrategy);
}
configure( cfg );
addConfigurationProperties(configurationProperties);
serviceRegistry = (BasicServiceRegistryImpl) new ServiceRegistryBuilder( configValues ).buildServiceRegistry();
cfg = new Ejb3Configuration();
configure(cfg);
cfg.configure(configurationProperties);
serviceRegistry = createServiceRegistry(cfg);
emf = cfg.buildEntityManagerFactory( serviceRegistry );
newEntityManager();
}
private BasicServiceRegistryImpl createServiceRegistry(Ejb3Configuration configuration) {
Properties properties = new Properties();
properties.putAll(configuration.getHibernateConfiguration().getProperties());
ConfigurationHelper.resolvePlaceHolders(properties);
return (BasicServiceRegistryImpl) new ServiceRegistryBuilder(properties).buildServiceRegistry();
}
@AfterClassOnce
public void close() {
closeEntityManager();

View File

@ -25,6 +25,7 @@ package org.hibernate.envers.test.integration.jta;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.EnversTestingJtaBootstrap;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.StrTestEntity;
import org.hibernate.envers.test.integration.reventity.ExceptionListenerRevEntity;
@ -33,6 +34,7 @@ import org.junit.Test;
import javax.persistence.EntityManager;
import javax.transaction.RollbackException;
import javax.transaction.TransactionManager;
import java.util.Properties;
import static org.hibernate.envers.test.EnversTestingJtaBootstrap.*;
@ -44,12 +46,16 @@ public class JtaExceptionListener extends AbstractEntityTest {
private TransactionManager tm;
public void configure(Ejb3Configuration cfg) {
tm = updateConfigAndCreateTM(cfg.getProperties());
cfg.addAnnotatedClass(StrTestEntity.class);
cfg.addAnnotatedClass(ExceptionListenerRevEntity.class);
}
@Override
public void addConfigurationProperties(Properties configuration) {
super.addConfigurationProperties(configuration);
tm = EnversTestingJtaBootstrap.updateConfigAndCreateTM(configuration);
}
@Test(expected = RollbackException.class)
@Priority(5) // must run before testDataNotPersisted()
public void testTransactionRollback() throws Exception {

View File

@ -10,6 +10,7 @@ import org.junit.Test;
import javax.persistence.EntityManager;
import javax.transaction.TransactionManager;
import java.util.Arrays;
import java.util.Properties;
import static org.hibernate.envers.test.EnversTestingJtaBootstrap.*;
@ -23,7 +24,12 @@ public class JtaTransaction extends AbstractEntityTest {
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(IntTestEntity.class);
tm = EnversTestingJtaBootstrap.updateConfigAndCreateTM(cfg.getProperties());
}
@Override
public void addConfigurationProperties(Properties configuration) {
super.addConfigurationProperties(configuration);
tm = EnversTestingJtaBootstrap.updateConfigAndCreateTM(configuration);
}
@Test

View File

@ -10,6 +10,7 @@ import org.junit.Test;
import javax.persistence.EntityManager;
import java.util.Arrays;
import java.util.Properties;
/**
* Tests simple auditing process (read and write operations) when <i>REVINFO</i> and audit tables
@ -21,10 +22,16 @@ public class DifferentDBSchemaTest extends AbstractEntityTest {
private Integer steId = null;
@Override
public void configure(Ejb3Configuration cfg) {
public void addConfigurationProperties(Properties configuration) {
super.addConfigurationProperties(configuration);
// Creates new schema after establishing connection
cfg.setProperty(Environment.URL, cfg.getProperties().getProperty(Environment.URL) + ";INIT=CREATE SCHEMA IF NOT EXISTS " + SCHEMA_NAME);
cfg.setProperty("org.hibernate.envers.default_schema", SCHEMA_NAME);
configuration.setProperty(Environment.URL, configuration.getProperty(Environment.URL) + ";INIT=CREATE SCHEMA IF NOT EXISTS " + SCHEMA_NAME);
configuration.setProperty("org.hibernate.envers.default_schema", SCHEMA_NAME);
}
@Override
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(StrTestEntity.class);
}