java.lang.IllegalStateException: There are two concurrently open transactions!
Asked by jdroidcoder:

-------------------------------------------------------

T E S T S

-------------------------------------------------------

Running tests.WorkWithDataBaseTest

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.802 sec \<\<\< FAILURE!

testGetMessage(tests.WorkWithDataBaseTest) Time elapsed: 0.648 sec \<\<\< ERROR!

java.lang.IllegalStateException: There are two concurrently open transactions!

at com.google.common.base.Preconditions.checkState(Preconditions.java:145)

at [net.java.ao](http://net.java.ao).DatabaseProvider.rollbackTransaction(DatabaseProvider.java:888)

at [net.java.ao](http://net.java.ao).test.junit.ActiveObjectTransactionMethodRule$1.evaluate(ActiveObjectTransactionMethodRule.java:85)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)

at org.junit.runners.ParentRunner.run(ParentRunner.java:236)

at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)

at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)

at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)

at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)

at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)

at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)

at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

Running ut.test.MyComponentUnitTest

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec

Results :

Tests in error:

testGetMessage(tests.WorkWithDataBaseTest): There are two concurrently open transactions!  

Help me, please  


Answers:
author: stadlerm
body: Hi,

I know this is quite old already, but in case somebody stumbles upon this - if you Debug your test code you will notice that the exception is actually raised after the test, when the ActiveObjects object is being destroyed, I guess it tries to rollback everything you haven't committed during the test or so, not exactly sure  

I was able to solve this issue by appending the @NonTransactional annotation to my test method - see also the Javadocs for ActiveObjectsJUnitRunner:

\* By default, each test method is executed within a separate transaction, which is then rolled back so the  
\* next test is executed with a fresh database state. If you annotate a test method with  
\* {@link NonTransactional @NonTransactional}, then it is not executed inside a transaction; instead, the  
\* database is completely reinitialized after executing the test.

Hope this helps someone :D

comments: author: Saurabh Gupta
body: How do you know that your code is performing transactionally? I want to use NonTransactionl annotation but then I don't know what is happening in the back end. Also, all my database calls are transactional.


author: Hugo Ingelmo
body: Cool!

You are right!


author: Nic Brough -Adaptavist-
body: Which Atlassian application is it, what are the tests, and what is your code doing that causes it?

comments: author: jdroidcoder
body: import static org.junit.Assert.\*;

import java.util.Date;  
import java.util.List;

import [net.java.ao](http://net.java.ao).test.converters.NameConverters;  
import [net.java.ao](http://net.java.ao).test.jdbc.Data;  
import [net.java.ao](http://net.java.ao).test.jdbc.DatabaseUpdater;  
import [net.java.ao](http://net.java.ao).test.jdbc.DynamicJdbcConfiguration;  
import [net.java.ao](http://net.java.ao).test.jdbc.Jdbc;  
import [net.java.ao](http://net.java.ao).test.junit.ActiveObjectsJUnitRunner;  
import [net.java.ao](http://net.java.ao).EntityManager;

import com.atlassian.activeobjects.external.ActiveObjects;  
import com.atlassian.activeobjects.test.TestActiveObjects;  
import com.atlassian.activeobjects.tx.Transactional;  
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;

import org.junit.Before;  
import org.junit.Test;  
import org.junit.runner.RunWith;  
import com.atlassian.sal.api.user.UserManager;

import ua.jdroidcoder.entitys.MessageEntity;  
import ua.jdroidcoder.workwithdatabase.IWorkWithDataBase;  
import ua.jdroidcoder.workwithdatabase.WorkWithDataBase;

@RunWith(ActiveObjectsJUnitRunner.class)  
@Jdbc(DynamicJdbcConfiguration.class)  
@Data(WorkWithDataBaseTest.ServiceImplTestDatabaseUpdater.class)  
public final class WorkWithDataBaseTest {  

private IWorkWithDataBase workWithDataBase;  
private EntityManager entityManager;  
@ComponentImport  
private ActiveObjects ao;  

@Before  
public void setUp() throws Exception{  
assertNotNull(entityManager);  
ao = new TestActiveObjects(entityManager);  
assertNotNull(ao);  
workWithDataBase = new WorkWithDataBase(ao);   
}  

@Test  
public void testGetMessage(){  

assertEquals(1, ao.find(MessageEntity.class).length);  
workWithDataBase.addNewMessage("test", "test", new Date(), "test", "test", "test");  
ao.flushAll();  
}  

public static class ServiceImplTestDatabaseUpdater implements DatabaseUpdater  
{  
@Override  
public void update(EntityManager em) throws Exception  
{  
em.migrate(MessageEntity.class);

final MessageEntity todo = em.create(MessageEntity.class);  
todo.setSubject("test");  
todo.setContent("test");  
todo.setImage("test");  
todo.setAuthorAvatar("test");  
todo.setAuthor("test");  
todo.setDate(new Date());  
todo.save();  
}  
}  
}


Tags: jira