• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Container Managed Transaction is not committing the Hibernate Insertion using EJB Session Bean

 
Barnabas Jeremiah
Greenhorn
Posts: 26
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...,

I am calling a method which is inserting datas into the data base.. I am using Hibernate for this operation and made it as a Container managed in my Stateless session EJB...but the Contaner's transaction is not visible to the hibernate session.save(objPerson);..
here is my hibernate.cfg.xml and code..



I am calling my dao from ejb...




 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barnabas,

Please edit your post and UseCodeTags, increasing code readability will increase the likelihood you get a response.
 
Barnabas Jeremiah
Greenhorn
Posts: 26
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Gorder wrote:Barnabas,

Please edit your post and UseCodeTags, increasing code readability will increase the likelihood you get a response.


Sorry and thanks Bill as I am a new comer to the Big Moose Saloon.. here after i will follow... thanks for your kind advice...
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barnabas,

EJB are not in my realm of experience but on first glance you are not binding the current session to the JTA session. See this document

https://community.jboss.org/wiki/SessionsAndTransactions#Transaction_demarcation_with_JTA

Note you are missing this part:



Now if you wanted to make use of declaritive transactions you could use this strategy

https://community.jboss.org/wiki/SessionsAndTransactions#Transaction_demarcation_with_EJBCMT

Then you could do something more like what you tried in your code, but you would have to edit the hibernate.transaction.factory_class per the document as well though.


You also should not create this every operation



This is expensive see this portion of the document

https://community.jboss.org/wiki/SessionsAndTransactions#What_about_the_SessionFactory



Also you should not be creating an insert statement like you are. Just create a person object and pass it to save. Hibernate will take care of inserting it into your database.

Lasty in your persistence unit do you have transaction-type="JTA" ?
 
Barnabas Jeremiah
Greenhorn
Posts: 26
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill,
My Transaction Type in the EJB is Container manager so i dont want to give commit in my business logic since Container takes care of it.. but in the case of Hibernate the Container does not commits the transaction from the EJB even though I have added the JTATransactionFactory and jta property values in the hibernate.cfg.xml....


Bill Gorder wrote:Thanks Barnabas,

EJB are not in my realm of experience but on first glance you are not binding the current session to the JTA session. See this document

https://community.jboss.org/wiki/SessionsAndTransactions#Transaction_demarcation_with_JTA

Note you are missing this part:



Now if you wanted to make use of declaritive transactions you could use this strategy

https://community.jboss.org/wiki/SessionsAndTransactions#Transaction_demarcation_with_EJBCMT

Then you could do something more like what you tried in your code, but you would have to edit the hibernate.transaction.factory_class per the document as well though.


You also should not create this every operation



This is expensive see this portion of the document

https://community.jboss.org/wiki/SessionsAndTransactions#What_about_the_SessionFactory



Also you should not be creating an insert statement like you are. Just create a person object and pass it to save. Hibernate will take care of inserting it into your database.

Lasty in your persistence unit do you have transaction-type="JTA" ?
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, when quoting replies please quote only the relevant parts of the text.

I think you might be misunderstanding how this works. Whether your transactions are container or application managed is irrelevant as far as this is concerned. Your application must define when it wants a transaction and where the transaction boundaries are, there is no way for the container to know this without your application telling it.

Take an example. Say I have a method that has 2 save operations. In one scenario I want each save to commit separately in its own transaction and in the other case I want them to commit together in one single transaction. How does the container know which to do? The answer is you have to tell it. Now there are 3 options:

1. When setting up your datasource in your container you can set auto-commit to true. In this case every statement will automatically be committed after it is executed. Usually this is not desirable so you typically would not do this.
2. You set it up the way you have it. In this case you must get the transaction from the container (since it is container managed) and then tell it when to start the transaction and when to commit it. This way you can handle both the scenarios I mentioned above.
3. You can use the declarative approach which I mentioned in another link in my last post. Here you annotate your method with @TransactionAttribute. This does some stuff behind the scenes for you. It gets the transaction for you and begins it for you when the method is entered. All of the code within that method is executed as a part of that same transaction, then when the method exits the transaction is committed.

If you are going to use what you have configured currently you have no choice but to put the transaction demarcation code in the data access code. If you wish to get rid of it then you need to go with #3 and re-read that part from my last post.


Also to make things a little bit simpler have a look at this too:

http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch01.html#d5e199
 
Barnabas Jeremiah
Greenhorn
Posts: 26
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill,for your valuable reply.. i will try... sorry for the misunderstanding...
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barnabas Jeremiah wrote: sorry for the misunderstanding


No need to be sorry I hope I helped.

Good luck!
 
Barnabas Jeremiah
Greenhorn
Posts: 26
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic