Tuesday, December 31, 2013

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.
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.
Hibernate:- 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.
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");
        logger.info("SET NUMBERPLATE FOR 1");
        try {
        } catch (InterruptedException e) {
hibernate.connection.url is jdbc:derby:isolation;create=true and hibernate.connection.isolation is 2. 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.
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.
share|improve this question
What version if hsqldb are you using? –  sharakan Feb 23 at 3:56
hsqldb version 2.2.8 –  N.M. Feb 23 at 4:41
add comment
Your expectation is wrong. Repeatable read doesn't mean that a transaction should block while another transaction reads the same row. It means that reading the same row twice in the same transaction should return the same data, even if another transaction has committed a change to this row between the first and the second read.
You get that (more or less) for free with Hibernate, since the second read will return the data from the first-level cache, without going to the database at all.
Also, you're making your test with a database that doesn't support this isolation level:
share|improve this answer
Nitpicky: he'd be hitting second level cache, right? Each thread has its own session. –  sharakan Feb 23 at 13:50
@sharakan: No. A single thread, getting the same entity twice in the same transaction, would hit the database the first time, and the first-level cache the second time. It would thus have repeatable reads, since the first-level cache will return the same data that was returned the first time (unless some changes have been made to the entity between the reads, of course). –  JB Nizet Feb 23 at 13:52 
Agreed, but he has two seperate threads, each of which call .openSession, and therefore have separate first level caches. –  sharakan Feb 23 at 15:07
That's exactly my point. Each thread will have its own first level cache, and will thus be unaffected by what other threads do, which will lead to repeatable read inside a single thread, thanks to the first level cache not being shared with other threads. –  JB Nizet Feb 23 at 15:12
Ah, I see what you mean. I thought when you said this: "...since the second read will return the data from the first-level cache, without going to the database at all." you were referring to the read from the second thread in his example. My mistake. –  sharakan Feb 23 at 15:16
show 2 more comments


oakleyses said...

louis vuitton handbags, oakley sunglasses, louboutin, longchamp outlet, nike shoes, louis vuitton outlet stores, chanel handbags, burberry outlet, prada outlet, jordan shoes, tiffany and co, michael kors outlet, tory burch outlet, louis vuitton outlet, longchamp handbags, nike free, true religion jeans, michael kors outlet, kate spade outlet, polo ralph lauren outlet, tiffany and co, prada handbags, polo ralph lauren outlet, michael kors outlet, michael kors outlet, longchamp handbags, oakley sunglasses, ray ban sunglasses, kate spade handbags, burberry outlet, louis vuitton outlet, louboutin outlet, louboutin, coach factory outlet, air max, air max, coach outlet, gucci outlet, christian louboutin shoes, michael kors outlet, coach purses, ray ban sunglasses, michael kors outlet, louis vuitton, coach outlet store online, true religion jeans, oakley sunglasses cheap

oakleyses said...

ralph lauren, lululemon, air max, hollister, north face, nike air max, polo lacoste, vanessa bruno, timberland, vans pas cher, louboutin, louis vuitton, oakley pas cher, air max pas cher, nike roshe run, air max, true religion outlet, barbour, sac longchamp, air force, hollister, sac louis vuitton, nike free, polo ralph lauren, nike trainers, louis vuitton uk, nike roshe, sac hermes, longchamp, michael kors, sac burberry, sac guess, mulberry, new balance pas cher, converse pas cher, sac louis vuitton, hogan outlet, nike tn, north face, true religion outlet, ray ban pas cher, michael kors, air jordan, nike blazer, nike free pas cher, michael kors pas cher, abercrombie and fitch, ray ban sunglasses

oakleyses said...

mac cosmetics, mont blanc, marc jacobs, canada goose outlet, nike huarache, vans shoes, soccer jerseys, hollister, giuseppe zanotti, beats by dre, abercrombie and fitch, longchamp, insanity workout, celine handbags, bottega veneta, ghd, nfl jerseys, north face outlet, chi flat iron, ugg boots, birkin bag, ugg australia, canada goose, herve leger, ugg pas cher, rolex watches, valentino shoes, canada goose uk, canada goose, ferragamo shoes, canada goose, ugg boots, uggs outlet, north face jackets, soccer shoes, asics running shoes, new balance shoes, p90x, lululemon outlet, canada goose jackets, mcm handbags, instyler, babyliss pro, ugg, wedding dresses, jimmy choo outlet, reebok outlet, nike roshe run

oakleyses said...

parajumpers, karen millen, air max, converse, pandora charms, moncler, louboutin, moncler, links of london, lancel, juicy couture outlet, oakley, hollister, pandora charms, supra shoes, thomas sabo, canada goose, gucci, wedding dresses, timberland boots, swarovski crystal, air max, coach outlet store online, moncler, ray ban, canada goose, moncler, ugg, louis vuitton, swarovski, hollister, montre homme, moncler, hollister clothing store, ralph lauren, rolex watches, moncler outlet, moncler, iphone 6 cases, baseball bats, juicy couture outlet, toms shoes, vans, pandora jewelry, ugg, converse shoes