java - Creating multiple transactions in a single hibernate session -
i have created quartz job runs in background in jboss server , responsible updating statistical data @ regular interval (coupled database flags)
to load , persist using hibernate 4. works fine except 1 hick up.
the entire thread i.e. job wrapped in single transaction on period of time (as amount of data increases) becomes huge , worry some. trying break single large transaction multiple small ones, such each transaction process sub group of data.
problem: tried lamely wrap code loop , start/end transaction @ start/end of loop. expected didn't work. have been looking around various forums figure out solution have not come across indicates managing multiple transaction in single session (where in 1 transaction active @ time).
i relatively new hibernate , appreciate points me direction on achieving this.
update: adding code demonstrate trying achieve , mean when breaking multiple transaction. , stack trace when executed.
log.info("starting calculation job."); list<groupmodel> groups = collections.emptylist(); daofactory hibdaofactory = null; try { hibdaofactory = daofactory.hibernate(); hibdaofactory.begintransaction(); organizationdao groupdao = hibdaofactory.getgroupdao(); groups = groupdao.findall(); hibdaofactory.committransaction(); } catch (exception ex) { hibdaofactory.rollbacktransaction(); log.error("error in transaction", ex); } try { hibdaofactory = daofactory.hibernate(); statsdao statsdao = hibdaofactory.getstatsdao(); statsscaledvaluesdao statsscaleddao = hibdaofactory.getstatsscaledvaluesdao(); (groupmodel grp : groups) { try { hibdaofactory.begintransaction(); log.info("performing computation group " + grp.getname() + " [" + grp.getid() + "]"); list<stats> statsdetail = statsdao.loadstatsgroup(grp.getid()); // coputing steps here (entry origentry : statsentries) { entry.setcalculateditem1(origentry.getcalculateditem1()); entry.setcalculateditem2(origentry.getcalculateditem2()); entry.setcalculateditem3(origentry.getcalculateditem3()); statsdetailsscaledvalues scvalues = entry.getscaledvalues(); if (scvalues == null) { scvalues = new statsdetailsscaledvalues(); scvalues.setid(origentry.getscrentryid()); scvalues.setvalues(origentry.getscaledvalues()); } else { scvalues.setvalues(origentry.getscaledvalues()); } statsscaleddao.makepersistent(scvalues); } hibdaofactory.committransaction(); } catch (exception ex) { hibdaofactory.rollbacktransaction(); log.error("error in transaction", ex); } { } } } catch (exception ex) { log.error("error", ex); } { } log.info("job complete.");
following exception stacktrace getting upon execution of job
org.hibernate.sessionexception: session closed! @ org.hibernate.internal.abstractsessionimpl.errorifclosed(abstractsessionimpl.java:127) @ org.hibernate.internal.sessionimpl.createcriteria(sessionimpl.java:1555) @ sun.reflect.generatedmethodaccessor469.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) @ java.lang.reflect.method.invoke(unknown source) @ org.hibernate.context.internal.threadlocalsessioncontext$transactionprotectionwrapper.invoke(threadlocalsessioncontext.java:352) @ $proxy308.createcriteria(unknown source) @ com.blueoptima.cs.dao.impl.hibernate.generichibernatedao.findbycriteria(generichibernatedao.java:132) @ com.blueoptima.cs.dao.impl.hibernate.scrstatsmanagementhibernatedao.loadstatsentriesfororg(scrstatsmanagementhibernatedao.java:22) ... 3 more
to understanding have read far hibernate, sessions , transactions. seems when session created attached thread , lives through out threads life or when commit or rollback called. thus, when first transaction committed session being closed , unavailable rest of threads life.
my question remains: how can have multiple transactions in single session?
more detail great , examples think should able have written here.
have 1 static sessionfactory (this big on memory) transactions want this.
someclass object = new someclass(); session session = sessionfactory().opensession() // create session object session.begintransaction(); //begins transaction session.save(object); // saves object remember isn't saved till session.commit() session.gettransaction().commit(); // persisting object session.close(); //closes transaction
this how used transaction, not sure if many transaction have @ time. session object light weight compared sessionfactory in memory.
if want save more objects @ time in 1 transaction example.
someclass object1 = new someclass(); someclass object2 = new someclass(); someclass object2 = new someclass(); session.begintransaction(); session.save(object1); session.save(object2); session.save(object3); session.gettransaction().commit(); // when commit called save 3 objects session.close();
hope in way or points in right direction. think configure program condense transactions well. :)
edit here great youtube tutorial. guy broke down me.
Comments
Post a Comment