Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6674449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:44:47+00:00 2026-05-26T03:44:47+00:00

When I run my unit tests in isolation they work fine ie. (omitted the

  • 0

When I run my unit tests in isolation they work fine ie. (omitted the asserts)

@Test
public void testSave()
 {
EntityManagerHelper emh = new EntityManagerHelper();
LevelDAO dao = new LevelDAO();
Level l = new Level();
l.setName("aname");
emh.beginTransaction();
dao.save(l);
emh.commit();
}

then running this individual test below no problem

@Test
public void testUpdate()
 {
EntityManagerHelper emh = new EntityManagerHelper();
LevelDAO dao = new LevelDAO();
Level l = new Level();
l.setName("bname");
l.setLevelid(1);
emh.beginTransaction();
dao.update(l);
emh.commit();
}

When they run at same time in sequence I recieve that error – Transaction is currently active. Is there a way to allow each unit test to run only after a transaction from previous piece of work is not active? Should I be looking at Spring instead?

Update

The EntityManagerHelper gains access to the persistence context like so

emf = Persistence.createEntityManagerFactory("bw_beta");        
threadLocal = new ThreadLocal<EntityManager>();

which looks like the problem

So a hacky workaround was to use define locally ie.

    EntityManagerFactory factory = Persistence.createEntityManagerFactory("bw_beta");
    EntityManager entityManager = factory.createEntityManager();
    entityManager.getTransaction().begin();
    dao.save(l);
    entityManager.persist(l);
    entityManager.getTransaction().commit();

Pretty sure there’s a better way – maybe using Spring?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:44:48+00:00Added an answer on May 26, 2026 at 3:44 am
    • Pretty sure there’s a better way – maybe using Spring?

    Yes, Spring cleans it up a lot and gives you control on what you’d like to run within a transaction without polluting the actual test.

    With Spring, your tests would look something like this:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({ "classpath:META-INF/conf/spring/application-context.xml",
                            "classpath:META-INF/conf/spring/test-datasource-spring-config.xml" })
    @TransactionConfiguration(transactionManager="txMgr", defaultRollback=false)
    public class LevelDaoTest {
    
        @Resource( name="levelDao" )
        LevelDao levelDao;
    
        @Test
        public void shouldSaveNewLevels() {
    
            Level l = new Level();
            l.setName("aname");
            levelDao.save(l);
            // assert
        }
    
        @Test
        public void shouldUpdateExistingLevels() {
    
            Level l = new Level(); // or I would assume, you'd read this level back from DB, or set a proper ID, so the DAO will know to update it.. But that is besides the point
            l.setName("bname");
            levelDao.update(l);
            // assert
        }
    }
    

    Take a look at Spring Documentation under Testing => Transaction Management to get more details.

    P.S. From your example:

    dao.save(l);
    entityManager.persist(l);
    

    Looks really strange, as usually you would encapsulate entityManager within a DAO, so all you’d need to do is dao.save(l)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run unit tests, I use _.P(My Test Message); to print information to
Consider this unit test: [TestMethod] public void QueueManager_OnNextItem() { bool called = false; var
I'm using the surefire maven plugin to run unit tests. My test class looks
Assuming Visual Studio.NET 2008 and MsTest are used to run unit tests. When a
I would like to accomplish two things during my build process: Run unit tests
We want to run our unit tests on our TFS server. We are running
I am trying to run some unit tests in a C# Windows Forms application
I am attempting to run some unit tests in python from what I believe
What is the best way to run silverlight unit tests automatically using team city?
I am modifying a Nant build script to run some unit tests. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.