multithreading - Dealing with deadlocks in long-running Hibernate transactions -


i have hibernate application may produce concurrent inserts , updates (via session.saveorupdate) records same primary key, assigned. these transactions long-running, perhaps 15 seconds on average (since data collected remote sources , persisted comes in). db isolation level set read committed, , i'm using mysql , innodb.

the problem scenario creates excessive lock waits timeout, either result of deadlock or long transactions. leads me few questions:

  • does database engine release locks when transaction committed?
  • if case, should seek shorten transactions?
  • if so, practice use separate read , write transactions, write transaction made short , take place after of data gathered (the bulk of transaction length involves collecting remote data).

edit:

here's simple test approximates believe happening. since i'm dealing long running transactions, commit takes place long after first flush. illustrate situation left commit out of test:

@entity static class person {     @id     long id = long.valueof(1);     @version     private int version; }  @test public void updatetest() {     (int = 0; < 5; i++) {         new thread() {             public void run() {                 session s = sf.opensession();                 transaction t = s.begintransaction();                 person p = new person();                 s.saveorupdate(p);                 s.flush(); // waits...             }         }.run();     } } 

and queries expectantly produces, waiting on second insert:

select id, version person id=? insert person (version, id) values (?, ?) select id, version person id=? insert person (version, id) values (?, ?) 

that's correct, database releases locks when transaction committed. since you're using hibernate, can use optimistic locking, locks database long periods of time. essentially, hibernate suggest, separating reading , writing portions separate transactions. on write, checks data in memory has not been changed concurrently in database.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -