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 9042719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:34:04+00:00 2026-06-16T10:34:04+00:00

I am testing hibernate using TestNg run under IntelliJ. I have two tests in

  • 0

I am testing hibernate using TestNg run under IntelliJ.
I have two tests in suite and when suite is run then one of them fails. When I run them separately they succeed.
Also when I click “Run only failed” I get green.

I guess that body of the tests does not matter in this case but I present it here anyway. Stacktrace is below.

I tried adding singleThreaded parameter to test suite but that didn’t help. I also added AfterMethod which I thought should help but result was the same.
Any ideas what’s going on?

@Test(singleThreaded = true)
public class RatesPersistanceTest
{
private SessionFactory sessionFactory;

@BeforeMethod
private void before() throws Exception
{
    sessionFactory = new Configuration().configure().buildSessionFactory();
}

@AfterMethod
private void after() throws Exception
{
    sessionFactory.close();
}

@Test
public void should_save_rates_and_retrieve_them_with_success()
{
    Money standardPrice = Money.parse("USD 85");
    Money upchargeExtraPerson = Money.parse("USD 80");
    Money upchargeExtraBed = Money.parse("USD 75");
    Room room = getMeRoom();

    AvailabilityPeriod availabilityPeriod = new AvailabilityPeriod(DateTime.now(), DateTime.now().plusDays(5), true);
    Season season = new BasicSeason("season name", availabilityPeriod);
    Rate seasonRate = new SeasonRate(standardPrice, upchargeExtraPerson, upchargeExtraBed, room, season);

    Session session = sessionFactory.openSession();
    session.beginTransaction();

    session.save(room);

    session.save(season);

    session.save(seasonRate);

    session.getTransaction().commit();
    session.close();
}

@Test(expectedExceptions = org.hibernate.exception.ConstraintViolationException.class)
public void season_rate_should_violate_db_constraints_when_saved_without_season()
{
    Money standardPrice = Money.parse("USD 85");
    Money upchargeExtraPerson = Money.parse("USD 80");
    Money upchargeExtraBed = Money.parse("USD 75");
    Room room = getMeRoom();

    AvailabilityPeriod availabilityPeriod = new AvailabilityPeriod(DateTime.now(), DateTime.now().plusDays(5), true);
    Season season = new BasicSeason("season name", availabilityPeriod);
    Rate seasonRate = new SeasonRate(standardPrice, upchargeExtraPerson, upchargeExtraBed, room, null);

    Session session = sessionFactory.openSession();
    session.beginTransaction();

    session.save(room);

    session.save(season);

    session.save(seasonRate);

    session.getTransaction().commit();
    session.close();
}


private Room getMeRoom()
{
    final RoomName roomName = new RoomName("101");
    final RoomType roomType = new RoomType("cheap");
    final Money standardPrice = Money.parse("USD 100");
    final Money upchargeExtraPerson = Money.parse("USD 50");
    final Money upchargeExtraBed = Money.parse("USD 20");
    final RackRate rackRate = new RackRate(standardPrice, upchargeExtraPerson, upchargeExtraBed, null);
    final int maxExtraBeds = 2;
    final Occupancy occupancy = new Occupancy(4, 2);
    return new Room("C", roomName, roomType, rackRate, HousekeepingStatus.CLEAN, RoomAvailability.AVAILABLE, maxExtraBeds, occupancy);
}

}

stack trace short version:

org.hibernate.PessimisticLockException: Timeout trying to lock table "ROOMS"; SQL   statement:
insert into ROOMS
...
Caused by: org.h2.jdbc.JdbcSQLException: Timeout trying to lock table "ROOMS"; SQL  statement:
insert into ROOMS

hibernate config:

 <property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=0</property>
 <property name="hbm2ddl.auto">create-drop</property>

stack trace:

org.hibernate.PessimisticLockException: Timeout trying to lock table "ROOMS"; SQL   statement:
insert into ROOMS (prefix, housekeepingStatus, availability, typeName, maxExtraBeds,   standardPrice, upchargeExtraPerson, upchargeExtraBed, standard, maximum, name) values  (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [50200-168]
    at org.hibernate.dialect.H2Dialect$2.convert(H2Dialect.java:317)
     at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at $Proxy9.executeUpdate(Unknown Source)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2962)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3403)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1210)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:399)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at net.mklew.hotelms.persistance.RatesPersistanceTest.should_save_rates_and_retrieve_them_with_success(RatesPersistanceTest.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:673)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:842)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1166)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.runWorkers(TestRunner.java:1178)
at org.testng.TestRunner.privateRun(TestRunner.java:757)
at org.testng.TestRunner.run(TestRunner.java:608)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1158)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1083)
at org.testng.TestNG.run(TestNG.java:999)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:203)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:174)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:111)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.h2.jdbc.JdbcSQLException: Timeout trying to lock table "ROOMS"; SQL  statement:
insert into ROOMS (prefix, housekeepingStatus, availability, typeName, maxExtraBeds, standardPrice, upchargeExtraPerson, upchargeExtraBed, standard, maximum, name) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [50200-168]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
at org.h2.message.DbException.get(DbException.java:169)
at org.h2.message.DbException.get(DbException.java:146)
at org.h2.table.RegularTable.doLock(RegularTable.java:500)
at org.h2.table.RegularTable.lock(RegularTable.java:434)
at org.h2.command.dml.Insert.insertRows(Insert.java:123)
at org.h2.command.dml.Insert.update(Insert.java:84)
at org.h2.command.CommandContainer.update(CommandContainer.java:75)
at org.h2.command.Command.executeUpdate(Command.java:230)
at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:156)
at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:142)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 47 more
  • 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-06-16T10:34:05+00:00Added an answer on June 16, 2026 at 10:34 am

    I got back to that project and problem came back as well.

    The solution is the following:

    Change @BeforeMethod and @AfterMethod to @BeforeSuite and @AfterSuite

    It also means that data inserted in one test will be visible in another test at least I think that’s the case if I don’t put down hibernate between tests, but this at least works so I can deal with it.

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

Sidebar

Related Questions

I have a problem using Hibernate and PostgreSQL for production and HSQLDB for testing.
im using JPA2 with Hibernate 3.6.x I have made a simple testing on the
I have two jUnit test classes, one for testing my ItemService class, the other
I'm using Hibernate 4.0.1.Final. I'm having trouble binding a session when testing my service.
I'm using Hibernate 4.0.1.Final to write a standalone Java app. When testing my data
I'm using DBUnit for testing DAOs implemented with Hibernate and Spring. I put a
Hi I'm testing some CRUD operations in Nhibernate. I have 3 tests in this
I have build my data model using JPA and am using Hibernate's EntityManager 3
I'm using H2 with Hibernate to generate in-memory DB on the fly for unit-testing.
I have a Spring project using Hibernate. The DB is generated based on the

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.