Hi everyone:
I know if I save some data to database , I should use transaction like this:
Code:
Session session=HibernateUtil.currentSession();
Transaction t=session.beginTransaction();
User u=new User();
.....................
...............
session.save(u);
t.commit();
HibernateUtil.closeSession();
It means that I need have to use a transaction.
If I query database only, Should I use transaction?
For example:
Code:
Session session=HibernateUtil.currentSession();
Transaction t=session.beginTransaction(); // <----It is nessecery to
//begin
//transaction here?
List list=session.find("from User");
.....................
Broker b=(Broker)session.get(Broker.class,broker_id);
.....................
t.commit();
HibernateUtil.closeSession();
I think it should be changed to following:
Code:
Session session=HibernateUtil.currentSession();
List list=session.find("from User");
.....................
Broker b=(Broker)session.get(Broker.class,broker_id);
.....................
HibernateUtil.closeSession();
The later will be better I think. Is it correct? Thks!