java - Spring not giving current session without marking @Transactional -
java - Spring not giving current session without marking @Transactional -
using spring sessionfactory injection.
@suppresswarnings("unchecked") public void getdata() { system.out.println("%%%% hibernate fetching info %%%% "); session session = factory.getcurrentsession(); /* * seek { thread.sleep(10000); } grab (interruptedexception e) { // todo * auto-generated grab block e.printstacktrace(); } */ list<address> addresses = session.createquery("from address").list(); system.out.println("total connectins = " + factory.getstatistics().getconnectcount()); (address address : addresses) { system.out.println(address); } }
above code giving exception
org.hibernate.hibernateexception: no session found current thread
but when method marked @transactional it's working fine. don't want transaction enabled method.
am missing anything..?
thats how transactions work. marking method @transactional
, asking transactionmanager
create , start new transaction new transactional session sessionfactory
. without transaction, hibernate doesn't create session
database. reason creates transaction
if goes wrong, hibernate can roll things did within 1 transaction.
without @transactional
, hibernate assumes don't need interact database. suggest reading spring documentation before proceeding because seem quite confused how technology works. through understanding of going on increment effectiveness when writing these sorts of things.
java spring hibernate
Comments
Post a Comment