File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes How to configure Transaction in Spring and Hibernate Annotation?.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to configure Transaction in Spring and Hibernate Annotation?.." Watch "How to configure Transaction in Spring and Hibernate Annotation?.." New topic
Author

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

Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

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..

Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Apr 07, 2009
Posts: 489

@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

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Apr 07, 2009
Posts: 489

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

Joined: May 24, 2012
Posts: 8
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.
 
wood burning stoves
 
subject: How to configure Transaction in Spring and Hibernate Annotation?..
 
Similar Threads
How to define Hibernate SessionFactory with Spring applicationcontext.xml
Stateless SB and Synchronization
What else is Spring good for?
Spring and Hibernate
Make hibernate use JTA transaction in WAS 5.1