Note that there are some explanatory texts on larger screens.

plurals
  1. POTransaction isolation level REPEATABLE_READ using hibernate and jdbc
    primarykey
    data
    text
    <p>I am trying to develop a demo to cement my understanding of the REPEATABLE_READ isolation level. I have tried demos using hibernate and jdbc. JDBC works as expected, however hibernate does not.</p> <p>My expectation is that, if i read a database row within one session, and i try to write the same row from another session, it should block until the first session finishes.</p> <p><strong>Hibernate</strong>:- When i 'get' an object (row) in Thread 0 and sleep, and within Thread 1 we try to update the same object (row), Thread 1 should wait till Thread 0 commits.</p> <pre><code>class HibernateExample7_IsolationRR_Read_Write_Thread implements Runnable { private static final Logger logger = LoggerFactory.getLogger(HibernateExample7_IsolationRR_Read_Write_Thread.class); SessionFactory sessionFactory; int sleep; int newnumberplate; public HibernateExample7_IsolationRR_Read_Write_Thread(SessionFactory sessionFactory, int sleep, int newnumberplate) { this.sessionFactory = sessionFactory; this.sleep = sleep; this.newnumberplate = newnumberplate; } public void run() { Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); logger.info("TRYING TO GET VEHICLE 1"); Vehicle vid1 = (Vehicle) session.get(Vehicle.class, Integer.valueOf(1)); logger.info("GOT VEHICLE 1"); vid1.setNumberplate(this.newnumberplate); logger.info("SET NUMBERPLATE FOR 1"); try { Thread.sleep(sleep); } catch (InterruptedException e) { e.printStackTrace(); } tx.commit(); session.close(); logger.info("COMMITED"); } } </code></pre> <p>hibernate.connection.url is <strong>jdbc:derby:isolation;create=true</strong> and hibernate.connection.isolation is <strong>2</strong>. I make two objects of HibernateExample7_IsolationRR_Thread, and i create a scenario where Thread 0 gets the vehicle and sleeps for long time. Meanwhile Thread 1 tries to get and update Vehicle. It successfully commits even before Thread 0 commits.</p> <p>Due to the REPEATABLE_READ isolation level, shouldn't T1 wait on a commit. If not then it's a READ_COMMITED isolation level not a REPEATABLE_READ isolation level.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload