spring hibernate transaction could not commit -
i wrote dao in spring used write in struts think this
@autowired private sessionfactory sessionfactory; session session=null; transaction tx=null; list<login> users= null; try{ session=sessionfactory.getcurrentsession(); tx=session.begintransaction(); users=session.createquery("from login").list(); tx.commit(); }catch(exception e){system.out.println("commit exception:"+e); try {tx.rollback();} catch (exception ex) {system.out.println("rollback exception:"+ex);} }finally{if(session!=null && session.isopen()){session.close();}}
but getting error:
threw exception [request processing failed; nested exception org.springframework.transaction.transactionsystemexception: not commit hibernate transaction; nested exception org.hibernate.transactionexception: transaction not started] root cause org.hibernate.transactionexception: transaction not started
can please me out.
if writing this,
try{ users=sessionfactory.getcurrentsession().createquery("from login").list(); }catch(exception e){system.out.println("commit exception:"+e);
its working fine, safe?.
thanks , regards
you're using @transactional, have spring start, commit , rollback transactions you, , handle transactions declaratively. whole point of precisely not have start, commit , rollback transactions in code. method implementation should be
return (list<login>) sessionfactory.getcurrentsession().createquery("from login").list();
Comments
Post a Comment