简体   繁体   中英

Using Hibernate and JDBC together from different threads

I want to use Spring-Hibernate and JDBC together in my application.

Hibernate should do all the updating and writing from one thread and other threads should just be able to read from the database without too much synchronization effort.

Will those JDBC-using threads deliver correct results (if they read from the database a short time after calling persist() or merge() ) or could it happen, that Hibernate has not flushed any updates and therefore other threads return wrong database entries?

"Wrong" depends on the isolation level you set for your connection pool.

I think it can work if Hibernate and Spring share the same connection pool and you set the isolation level to SERIALIZABLE for all connections.

Long-running transactions will be the problem. If all your write operations are fast you won't block. If you don't commit and flush updates quickly the read operations will either have to block and wait OR allow "dirty reads".

That depends. You're basically describing a race condition - if you want to make sure that your read-thread only reads after the write-thread has persisted, you will have to look into thread synchronization methodology.

Cheers,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM