How to set-up transactions for both web application and batch jobs using Spring and Hibernate -


i have application uses spring 2.5 , hibernate 3.

there's web application presentation layer, servive layer , dao layer, quartz jobs sharing same service , dao layers.

transactions initialized in different layers @transactional annotations, this:

alt text

it led me problem described here: controlling inner transaction settings outer transaction spring 2.5

i read bit how set-up transactions wire spring , hibernate together. looks recommended approach initialize transactions in service layer.

what don't transactions exist because required hibernate work properly.

and when need transaction job calling multiple service methods, seems don't have choice keep initializing transactions jobs. moving @transactional annotations dao service doesn't seem make difference.

how recommend set-up transactions kind of application?

i read bit how set-up transactions wire spring , hibernate together. looks recommended approach initialize transactions in service layer.

definitely. transaction demarcation should done @ service layer level, not @ dao layer level:

  • the unit of work service, not dao
  • you want transaction span multiple daos if required.

what don't transactions exist because required hibernate work properly.

you should maybe elaborate part because transactions not hibernate specific.

and when need transaction job calling multiple service methods, seems don't have choice keep initializing transactions jobs.

if want call multiple services inside transaction initiated job layer, declare services transactional required semantics (the default) , rely on spring transaction propagation (this applies unless need remote call; in case, use ejbs).

so moving @transactional annotations dao service doesn't seem make difference.

it does make difference , fact need initiate transactions job layer when running batches doesn't make things different.

i warmly recommend read chapter 9. transaction management.


(...) main problem comes hibernate. sorry if wasn't clear.

no problem. it's when question vague, vague answer :)

from hibernate documentation: "database transactions never optional. communication database has occur inside transaction.". that's why developers put dao methods transactional on project.

sorry above statement says "communication database has occur inside transaction", nothing more, , deciding start transaction left @ discretion (typically service layer). if @ dao level, if mysuperservice calls daofoo , daobar , daobar fails? in such cases, you'll want rollback changes, not performed in daobar. hence need control transaction unit of work starts.

imho, developers need guidance.

does mean services should transactional? when read data example?

first of all, suggest read non-transactional data access , auto-commit mode (the little brother of sessions , transactions) clarify things "read-only transactions". reading whole page worth let me quote specific part:

many application developers think can talk database outside of transaction. isn’t possible; no sql statement can send database outside of database transaction. term nontransactional data access means there no explicit transaction boundaries, no system transaction, , behavior of data access of autocommit mode. doesn’t mean no physical database transactions involved.

once you'll done above link, next suggested reading @transactional read-only flag pitfalls. here relevant part:

(...) bottom line when use orm-based framework, read-only flag quite useless , in cases ignored. if still insist on using it, set propagation mode supports, shown in listing 9, no transaction started:

listing 9. using read-only , supports propagation mode select operation

@transactional(readonly = true, propagation=propagation.supports) public tradedata gettrade(long tradeid) throws exception {    return em.find(tradedata.class, tradeid); } 

better yet, avoid using @transactional annotation altogether when doing read operations, shown in listing 10:

listing 10. removing @transactional annotation select operations

public tradedata gettrade(long tradeid) throws exception {    return em.find(tradedata.class, tradeid); } 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -