What is the role of session.beginTransaction() of hibernate in web applications and standalone applications.
I am aware that session.beginTransaction() is used to define the transaction boundaries. When I execute a simple hibernate application (standalone), If i dont give this line
Transaction tx = session.beginTransaction();
//code to save data
Then data is not getting saved into db. But if i mention above lines in my code everything works fine for standalone java class,
Whereas the same is not applicable for web applications when i deploy it on Jboss, Irrespective of session.beginTransaction() data saves into DB.
Dont we need to define the session.beginTransaction() for webapplications.
Thanks for your reply.
I have already gone through the information that you linked it over here. I am not using EJB in my sample application. I am just deploying it in JBOSS. The result remains same either the transaction boundaries are defined or not. Whereas with standalone apps when i don't define my transaction boundaries then Data is not getting saved into the database
The information that you linked in your post has information related to CMT and BMT. When CMT is used we dont need to define the transaction boundaries(Transaction Demarcation)
Even if You ommit session.beginTransaction(), that doesnt mean You are talking to a database outside of a transaction.
This is because SQL statement can not be sent to a database outside of a database transaction.
It is so called nontransactional data access, and it means there are no explicit transaction boundires.
Behaviour of data access is in relation to autocomit mode.
In case You are working nontransactional, there is no transactional atomicity of a group of SQL statements.
There is also problem if data is modified concurrently.
So, let say You are working nontrasactional:
Session session = sessionFactory.openSession();
What happens is:
1. New Session is opened. No database connections are obtained yet.
2. get() trigers an SQL Select. Session obtains database connection. Hibernate turns off the autocommit mode on this connection. Transaction is started.
3. Select is executed inside transaction. Session is closed. Connection is returned to the pool and released by Hibernate.
What happens to the uncommitted transaction, depends.
In most cases, call to close() commits transaction.
There is a problem, when working with Insert statement (session.save()), and that is another story.
Its no good idea to work nontransactional.
Joined: Oct 13, 2010
Thanks a lot for your explanation. But my problem was, When i use hibernate with managed environment like jboss, even when there is no session.begintransaction(), data gets saved/updated in my database, but in non managed environments(Standalone and tomcat) if there is no session.begintransaction(), Data is not getting saved/updated in Db.
FYI: I am using the same database in both managed and non managed environments
I suspect that JBoss which is a managed environment is using CMT by default. The following lines are taken from the book hibernate reference. Please comment on the same
JDBC connections obtained from a JNDI datasource will automatically participate in the container-managed
transactions of the application server.
Since Hibernate in managed environments like Jboss uses JNDI datasource along with JDBC drivers to connect to the database , I suspect this might be the reason.
Joined: Feb 23, 2012
First, the hibernate.connection.autocomit configuration has no effect in Java EE enviroment with JTA and CMT.
Second, default FlushMode of a Session changes when no transaction is in progress(we are talking about nontransactional operations).
Some interesting tips to know:
Default FlushMode.Auto results in a sinhronization before every HQL, SQL or Criteria query.
Because of that, DML operations execute in adition to a SELECT for the query.
And, beucause You are working in autocomit mode(nontransactional, see the post from yesterday), these modifications are permanent.
Hibernate prevents this by disabling automatic flushing when you use a Session outside of transaction boundaries.
subject: Hibernate Session.beginTransaction with JBoss