• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate Session.beginTransaction with JBoss

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you look at this for information about Transactions in JEE server (JBoss in this case).
 
vShyam Sundar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like your application is using container managed transactions.
Are you sure you have configured bean managed transactions?
 
vShyam Sundar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
I can't take it! You are too smart for me! Here is the tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic