This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Transactions in Spring + Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Transactions in Spring + Hibernate" Watch "Transactions in Spring + Hibernate" New topic
Author

Transactions in Spring + Hibernate

Jean Jorge Michel
Greenhorn

Joined: Oct 29, 2008
Posts: 4
Hi guys!

I�m trying using Spring + Hibernate to persist system�s objects (Hibernate) and control transactions (Spring).
But every interaction with DB (MySQL) a new connection is opened, ignoring my pool (c3p0) configuration.

My example is so simple, User and Note objects to persist.
My example is organized in three packages:

Model
There are two classes:
User
Note

These objects are annotated to Hibernate.

DAO
There are three interfaces:
GenericDAOInterface
UserDAOInterface
NoteDAOInterface

And three implementations:
SpringDAO extends HibernateDaoSupport
and implements GenericDAOInterface
UserDAO extends SpringDAO
and implements UserDAOInterface
NoteDAO extends SpringDAO
and implements NoteDAOInterface

These objects are annotated with @Repository.

Facade
There are two interfaces:
UserFacadeInterface
NoteFacadeInterface

And two implementations:
UserFacade implements UserFacadeInterface
NoteFacade implements NoteFacadeInterface

These objects are annotated with @Transactional.

When I use the system this work fine, I can persiste my beans. I look in MySQL and my system start the first operation (login) creating three connections and to every operation with DB (list my notes, view register/view page with correct register data filed, etc) is opened a connection in MySQL.

I tried create a TransactionManager, TransactionInterceptor, etc� into my applicationContext.xml but I cant�t make Spring use my transaction configuration!!! I tried put in dataSource bean a declararion type: <property name="transactionManager"><ref local="transactionManager"/></property> but don�t work or in Hibernate properties: <prop key="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>.

Someone can help me? My config file is:

Satish Kandagadla
Greenhorn

Joined: Jul 03, 2006
Posts: 27
Are you trying to use transactions driven by annotations?

If yes please try to change the bean <tx:annotation-driven/> to this

<tx:annotation-driven transaction-manager="transactionManager"/>
Jean Jorge Michel
Greenhorn

Joined: Oct 29, 2008
Posts: 4
Hi,
Is not it, do not work ;(

I am looking in MySQL console (Windows� command prompt) and for all hit is open a new connection
This problem will kill my project, if I do not able to control this problem Spring + Hibernate will be removed from the project and I will be forced to use procedures + JDC (noooooo!!!).

Someone have any idea to help me?

Thanks.

My new attempt is:

Satish Kandagadla
Greenhorn

Joined: Jul 03, 2006
Posts: 27
Originally posted by Jean Jorge Michel:
Hi,
Is not it, do not work ;(

I am looking in MySQL console (Windows� command prompt) and for all hit is open a new connection
This problem will kill my project, if I do not able to control this problem Spring + Hibernate will be removed from the project and I will be forced to use procedures + JDC (noooooo!!!).

Someone have any idea to help me?

Thanks.

My new attempt is:



Your transaction beans are so confusing. Are you using all the beans in the configuration that you pasted above? I would first clean up the unused beans and give it a try.
Jean Jorge Michel
Greenhorn

Joined: Oct 29, 2008
Posts: 4
Satish, thanks for your attention.

I am new Spring�s user (of course hahaha), and this configuration is the result of hours and hours fighting with Spring and search on Google a silver bullet to save me.

Today on morning (now in Rio de Janeiro/Brazil is 10: 50AM) I tried:



And the problem is the same.

Let me show �all problem�.

I Start MySQL DB and log in, is created 2 connections.
When I start my Tomcat and do login my pool is initialized and is created three new connections.
After login my JSF navigation rule redirect the browser to home, this page search on DB my notes and show it in a table. In this point is created more three connections. Now I have 8 connections with MySQL.

My transaction management is ignored or my pool is crazy =)

Here is my codes.

First login flow.



Second home flow:



Some idea?

Thanks for all help.
Satish Kandagadla
Greenhorn

Joined: Jul 03, 2006
Posts: 27
Originally posted by Jean Jorge Michel:
Satish, thanks for your attention.

I am new Spring�s user (of course hahaha), and this configuration is the result of hours and hours fighting with Spring and search on Google a silver bullet to save me.

Today on morning (now in Rio de Janeiro/Brazil is 10: 50AM) I tried:



And the problem is the same.

Let me show �all problem�.

I Start MySQL DB and log in, is created 2 connections.
When I start my Tomcat and do login my pool is initialized and is created three new connections.
After login my JSF navigation rule redirect the browser to home, this page search on DB my notes and show it in a table. In this point is created more three connections. Now I have 8 connections with MySQL.

My transaction management is ignored or my pool is crazy =)

Here is my codes.

First login flow.



Second home flow:



Some idea?

Thanks for all help.


I do not see anything wrong with the transaction beans. How are you saying that the transaction attributes are not working? What are you expecting from the annotations given in your application?
Jean Jorge Michel
Greenhorn

Joined: Oct 29, 2008
Posts: 4
Guys, I resolved this error.

In a Brazilian JUG e-mail list (RSJUG) one participant named Julio told me that I should start Spring`s context together my application and not instantiate in every backbean`s method an ApplicationContext object. It was my error, creating always a new instance of my pool, etc�

Searching in Google I found how make it and now all it is working fine

My web.xml:



In faces-config.xml I defined that the backbean receive a facade.



Set how resolves this EL expression

In faces-config.xml too:



In login backbean have an attribute named userFacade, getters and setters to this and none instantiate code of ApplicationContext object, Spring inject it to me.

Thanks for all answers.
 
Consider Paul's rocket mass heater.
 
subject: Transactions in Spring + Hibernate
 
Similar Threads
Dependency injection problem
data not persisted using hibernate spring integration
how does rollbackFor work ?
org.hibernate.MappingException: Unknown entity:
HibernateDaoSupport and Transaction