GeeCON Prague 2014*
The moose likes Object Relational Mapping and the fly likes Hibernate Session.beginTransaction with JBoss Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Session.beginTransaction with JBoss" Watch "Hibernate Session.beginTransaction with JBoss" New topic
Author

Hibernate Session.beginTransaction with JBoss

vShyam Sundar
Ranch Hand

Joined: Oct 13, 2010
Posts: 60
Hi,
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
tx.commit();
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.

Awaiting for your valuable responses.



Thanks,
Shyam.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3833

I suggest you look at this for information about Transactions in JEE server (JBoss in this case).


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
vShyam Sundar
Ranch Hand

Joined: Oct 13, 2010
Posts: 60
Hi,
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)


Thanks,
Shyam.


E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Sounds like your application is using container managed transactions.
Are you sure you have configured bean managed transactions?
vShyam Sundar
Ranch Hand

Joined: Oct 13, 2010
Posts: 60
Hi,
My application is a simple test application. Where my servlet calls a java class which initiates the hibernate. I am not using EJB in my application.




Thanks,
Shyam.
Darko Ristic
Greenhorn

Joined: Feb 23, 2012
Posts: 6
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();
session.get(Trt.class, 1);
session.close();

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.
vShyam Sundar
Ranch Hand

Joined: Oct 13, 2010
Posts: 60
Hi Diamond,
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.



Thanks,
Shyam.
Darko Ristic
Greenhorn

Joined: Feb 23, 2012
Posts: 6
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.
 
GeeCON Prague 2014
 
subject: Hibernate Session.beginTransaction with JBoss