You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Luke McCollum <lu...@sacramentopress.com> on 2010/04/23 20:51:13 UTC

Unit Testing Transactions ... what the?

So I'm attempting to get some unit tests running using Open EJB and Hibernate. I had planned on rolling back transactions after running unit tests, but for whatever reason, transactions don't roll back at all. I set up a dead simple test class to extend and ended up making even more dead simple to try to test transactions. Still can't get it going. Here's the class:

public class AbstractEJBIntegrationTester {

    protected static InitialContext context;
    protected static DeploymentInitializationService deploymentInitializationService;
    private static UserTransaction userTransaction;


    @BeforeClass
    public static void before() throws Exception {
        System.setProperty("hibernate.hbm2ddl.auto", "create");
        System.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        System.setProperty("openejb.logger.external", "true");
        Properties properties = new Properties();
        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
        URL config = AbstractEJBIntegrationTester.class.getClassLoader().getResource("META-INF/openejb-test.xml");
        properties.setProperty("openejb.configuration", config.toExternalForm());
        context = new InitialContext(properties);
        userTransaction = (UserTransaction)context.lookup("java:comp/UserTransaction");
        userTransaction.begin();
        deploymentInitializationService = (DeploymentInitializationService) context
                .lookup("DeploymentInitializationServiceBean");
        deploymentInitializationService.runAll();
        userTransaction.rollback();
    }
    
    @Test
    public void noop() {
        assertTrue(true);
    }
    
}

Open EJB loads great, everything is created and runs properly...but my transaction never rolls back! I've gone so far as to turn on trace logging for all of the components, but Hibernate, Open EJB, and Geronimo all tell me that a transaction is being opened, joined, then rolled back successfully. For completeness, here is my openejb-test.xml file referred to above:

<?xml version="1.0" encoding="UTF-8"?>
<openejb>
	<SecurityService id="Default Security Service" />
	<TransactionManager id="Default Transaction Manager" />
	<Resource id="mysql_managed" type="DataSource">
		JdbcDriver com.mysql.jdbc.Driver
		JdbcUrl jdbc:mysql://localhost/testing
		UserName root
		JtaManaged true
	</Resource>
	<Resource id="Default JMS Resource Adapter" type="ActiveMQResourceAdapter" />
	<Resource id="jms/connectionFactory" type="javax.jms.ConnectionFactory" />
	<Resource id="Default Queue Connection Factory" type="QueueConnectionFactory" />
	<Resource id="Default Topic Connection Factory" type="TopicConnectionFactory" />
	<Resource id="jms/myQueue" type="javax.jms.Queue" />
</openejb>


I've been trying to get it working with HSQLDB instead of MySQL (they are unit tests after all), but I decided just to experiment with another DBMS to rule that out. HSQLDB has the exact same problem.

Re: Unit Testing Transactions ... what the?

Posted by Luke McCollum <lu...@sacramentopress.com>.
Wow, that did it. At least for HSQLDB. For some reason MySQL is still not rolling back transactions. But that's fine by me at the moment! Thank you for this! 

- Luke

P.S. I've actually run the test you mention there, substituting mysql jdbc parameters and it does work fine. Weird. Anyway, I'm just happy HSQL is performing my tests correctly.

On Apr 23, 2010, at 3:47 PM, David Blevins wrote:

> I've seen this once back in February.  It turned out that the user had another copy of commons-dbcp in their classpath.  The older versions of commons-dbcp doesn't support transactional connection pooling.  This seems like a classpath issue rather than a configuration issue.
> 
> Hunt for that, if it turns out to be the issue.  We can see if we can hack together some sort of reflective inspector to look for this issue specifically and report it.
> 
> Anyway, after that user reported the issue I went ahead and created this test case which demonstrates all forms of rollback:
> 
>  http://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples/transaction-rollback/
> 
> 
> -David
> 
> On Apr 23, 2010, at 11:51 AM, Luke McCollum wrote:
> 
>> So I'm attempting to get some unit tests running using Open EJB and Hibernate. I had planned on rolling back transactions after running unit tests, but for whatever reason, transactions don't roll back at all. I set up a dead simple test class to extend and ended up making even more dead simple to try to test transactions. Still can't get it going. Here's the class:
>> 
>> public class AbstractEJBIntegrationTester {
>> 
>>   protected static InitialContext context;
>>   protected static DeploymentInitializationService deploymentInitializationService;
>>   private static UserTransaction userTransaction;
>> 
>> 
>>   @BeforeClass
>>   public static void before() throws Exception {
>>       System.setProperty("hibernate.hbm2ddl.auto", "create");
>>       System.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
>>       System.setProperty("openejb.logger.external", "true");
>>       Properties properties = new Properties();
>>       properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
>>       URL config = AbstractEJBIntegrationTester.class.getClassLoader().getResource("META-INF/openejb-test.xml");
>>       properties.setProperty("openejb.configuration", config.toExternalForm());
>>       context = new InitialContext(properties);
>>       userTransaction = (UserTransaction)context.lookup("java:comp/UserTransaction");
>>       userTransaction.begin();
>>       deploymentInitializationService = (DeploymentInitializationService) context
>>               .lookup("DeploymentInitializationServiceBean");
>>       deploymentInitializationService.runAll();
>>       userTransaction.rollback();
>>   }
>> 
>>   @Test
>>   public void noop() {
>>       assertTrue(true);
>>   }
>> 
>> }
>> 
>> Open EJB loads great, everything is created and runs properly...but my transaction never rolls back! I've gone so far as to turn on trace logging for all of the components, but Hibernate, Open EJB, and Geronimo all tell me that a transaction is being opened, joined, then rolled back successfully. For completeness, here is my openejb-test.xml file referred to above:
>> 
>> <?xml version="1.0" encoding="UTF-8"?>
>> <openejb>
>> 	<SecurityService id="Default Security Service" />
>> 	<TransactionManager id="Default Transaction Manager" />
>> 	<Resource id="mysql_managed" type="DataSource">
>> 		JdbcDriver com.mysql.jdbc.Driver
>> 		JdbcUrl jdbc:mysql://localhost/testing
>> 		UserName root
>> 		JtaManaged true
>> 	</Resource>
>> 	<Resource id="Default JMS Resource Adapter" type="ActiveMQResourceAdapter" />
>> 	<Resource id="jms/connectionFactory" type="javax.jms.ConnectionFactory" />
>> 	<Resource id="Default Queue Connection Factory" type="QueueConnectionFactory" />
>> 	<Resource id="Default Topic Connection Factory" type="TopicConnectionFactory" />
>> 	<Resource id="jms/myQueue" type="javax.jms.Queue" />
>> </openejb>
>> 
>> 
>> I've been trying to get it working with HSQLDB instead of MySQL (they are unit tests after all), but I decided just to experiment with another DBMS to rule that out. HSQLDB has the exact same problem.
> 


Re: Unit Testing Transactions ... what the?

Posted by David Blevins <da...@visi.com>.
I've seen this once back in February.  It turned out that the user had  
another copy of commons-dbcp in their classpath.  The older versions  
of commons-dbcp doesn't support transactional connection pooling.   
This seems like a classpath issue rather than a configuration issue.

Hunt for that, if it turns out to be the issue.  We can see if we can  
hack together some sort of reflective inspector to look for this issue  
specifically and report it.

Anyway, after that user reported the issue I went ahead and created  
this test case which demonstrates all forms of rollback:

   http://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples/transaction-rollback/


-David

On Apr 23, 2010, at 11:51 AM, Luke McCollum wrote:

> So I'm attempting to get some unit tests running using Open EJB and  
> Hibernate. I had planned on rolling back transactions after running  
> unit tests, but for whatever reason, transactions don't roll back at  
> all. I set up a dead simple test class to extend and ended up making  
> even more dead simple to try to test transactions. Still can't get  
> it going. Here's the class:
>
> public class AbstractEJBIntegrationTester {
>
>    protected static InitialContext context;
>    protected static DeploymentInitializationService  
> deploymentInitializationService;
>    private static UserTransaction userTransaction;
>
>
>    @BeforeClass
>    public static void before() throws Exception {
>        System.setProperty("hibernate.hbm2ddl.auto", "create");
>        System.setProperty("hibernate.dialect",  
> "org.hibernate.dialect.MySQLDialect");
>        System.setProperty("openejb.logger.external", "true");
>        Properties properties = new Properties();
>        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,  
> "org.apache.openejb.client.LocalInitialContextFactory");
>        URL config =  
> AbstractEJBIntegrationTester 
> .class.getClassLoader().getResource("META-INF/openejb-test.xml");
>        properties.setProperty("openejb.configuration",  
> config.toExternalForm());
>        context = new InitialContext(properties);
>        userTransaction = (UserTransaction)context.lookup("java:comp/ 
> UserTransaction");
>        userTransaction.begin();
>        deploymentInitializationService =  
> (DeploymentInitializationService) context
>                .lookup("DeploymentInitializationServiceBean");
>        deploymentInitializationService.runAll();
>        userTransaction.rollback();
>    }
>
>    @Test
>    public void noop() {
>        assertTrue(true);
>    }
>
> }
>
> Open EJB loads great, everything is created and runs properly...but  
> my transaction never rolls back! I've gone so far as to turn on  
> trace logging for all of the components, but Hibernate, Open EJB,  
> and Geronimo all tell me that a transaction is being opened, joined,  
> then rolled back successfully. For completeness, here is my openejb- 
> test.xml file referred to above:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <openejb>
> 	<SecurityService id="Default Security Service" />
> 	<TransactionManager id="Default Transaction Manager" />
> 	<Resource id="mysql_managed" type="DataSource">
> 		JdbcDriver com.mysql.jdbc.Driver
> 		JdbcUrl jdbc:mysql://localhost/testing
> 		UserName root
> 		JtaManaged true
> 	</Resource>
> 	<Resource id="Default JMS Resource Adapter"  
> type="ActiveMQResourceAdapter" />
> 	<Resource id="jms/connectionFactory"  
> type="javax.jms.ConnectionFactory" />
> 	<Resource id="Default Queue Connection Factory"  
> type="QueueConnectionFactory" />
> 	<Resource id="Default Topic Connection Factory"  
> type="TopicConnectionFactory" />
> 	<Resource id="jms/myQueue" type="javax.jms.Queue" />
> </openejb>
>
>
> I've been trying to get it working with HSQLDB instead of MySQL  
> (they are unit tests after all), but I decided just to experiment  
> with another DBMS to rule that out. HSQLDB has the exact same problem.