Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to configure Transaction in Spring and Hibernate Annotation?..

 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends..

i have any question..
how to configure Transaction in application which developed in Spring and Hibernate?..
i use Spring just as IoC..

which one is good for implementation..
do i have to use JTA or i just use Embedded Transaction which provided by Spring?..

now.. i do like this one in Spring container..


and in My DAO :


is it can accepted as Simple Transaction?..
or i have to use JTA?..

is it any link for integrating between Spring IoC, Hibernate/JPA, and JTA?..

Thanks all..
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excerpt from Spring official doc. For more read on...
The Spring Framework's transaction management support significantly changes traditional thinking as to when a J2EE application requires an application server.

In particular, you don't need an application server just to have declarative transactions via EJB. In fact, even if you have an application server with powerful JTA capabilities, you may well decide that the Spring Framework's declarative transactions offer more power and a much more productive programming model than EJB CMT.

Typically you need an application server's JTA capability only if you need to enlist multiple transactional resources, and for many applications being able to handle transactions across multiple resources isn't a requirement.
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Adeel...

Thanks for your reply..

in that example, we can use Annotation @Transactional..
i have modified my application's configuration..
and shown like the following code :


is it configuration true?..
iam using sessionFactory and transaction manager..

and the DAO code is like this :


is it DAO have been configured as Transactional mode?..

iam sorry before.. iam still confusing about the rollBackFor Property..
i've been configure Transactional Annotation like this one :
@Transactional(readOnly=false,rollbackFor=Exception.class)

is it the rollback will executed if any errors occured?..
i just use rollbackFor=Exception.class, and not use specify another Exception classes, beacuse all of the Exception classes is inheritated of "java.lang.Exception"..

Thank's in advance
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need not define DataSourceTransactionManager, if you are using Hibernate, or you can think the other way around.

Well, if you define general Exception class, then it means you want to rollback the particular transaction in case any exception occurs. There are cases when you still want to commit the transaction if one kind of exceptions occur and rollback the transaction when the other kind of exceptions occur. I hope it made it clear now.
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Adeel for your post reply..

yeah iam using hibernate..
is it enough to make transaction just use the sessionFactory Hibernate?..
The Class extends HibernateDaoSupport..
and i use the following code :

getSession().beginTransaction().begin();
getSession().beginTransaction().commit();
getSession().beginTransaction().rollback();


Thanks in advance...
 
Brajesh Choudhary
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi leonardo

I am configuring hibernate in my spring project i hope you are able to configure.I have a question that you are using persistence.xml or not or there is any kind of entry in your web.xml file for your persistence.xml file.can you provide me your sample files which you use in your project just a basic one.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic