PDA

View Full Version : plain Hibernate 3 API and transaction advice



jbarbede
Mar 11th, 2008, 05:56 PM
I've a system that use a plain Hibernate 3 API (like found in Spring documentation: Implementing DAOs based on plain Hibernate 3 API (http://static.springframework.org/spring/docs/2.5.x/reference/orm.html#orm-hibernate)) to allow Spring to access to my data.

To manage transactions between Spring and Hibernate, I use declarative transaction management with transaction advice and AOP (Spring documentation A first example (http://static.springframework.org/spring/docs/2.5.x/reference/transaction.html#transaction-declarative)).



<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerD ataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/project"/>
<property name="username" value="..."/>
<property name="password" value="..."/>
</bean>

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFac toryBean">

<property name="dataSource" ref="myDataSource"/>

<property name="mappingResources">
<list>
<value>com/project/model/member/Member.hbm.xml</value>
<value>com/project/model/city/City.hbm.xml</value>
<value>com/project/model/meteostate/MeteoState.hbm.xml</value>
</list>
</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>

</bean>

<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="myTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransa ctionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>

<tx:advice id="txAdvice" transaction-manager="myTransactionManager">
<tx:attributes>

<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="serviceMethods" expression="execution(* com.livemeteo.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
</aop:config>

<!-- Member Service object-->
<bean id="myMemberService" class="com.project.service.member.MemberServiceImpl">
<property name="memberDao" ref="myMemberDao"/>
</bean>

<!-- Member DAO object -->
<bean id="myMemberDao" class="com.project.persistence.member.MemberDaoImpl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>

<!-- City Service object-->
<bean id="myCityService" class="com.project.service.city.CityServiceImpl">
<property name="cityDao" ref="myCityDao"/>
</bean>

<!-- City DAO object -->
<bean id="myCityDao" class="com.project.persistence.city.CityDaoImpl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>

<!-- MeteoState Service object-->
<bean id="myMeteoStateService" class="com.project.service.meteostate.MeteoStateServiceIm pl">
<property name="meteoStateDao" ref="myMeteoStateDao"/>
</bean>

<!-- MeteoState DAO object -->
<bean id="myMeteoStateDao" class="com.project.persistence.meteostate.MeteoStateDaoIm pl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>

</beans>


I think this configuration is correct but I've LazyInitialisationException when I make tests. So if I've well understood, I've problems with the lazy loading of Hibernate.

But I don't see what is missing in my configuration. The transaction advice doesn't aim to solve this problem?

jbarbede
Mar 12th, 2008, 02:42 AM
In fact, I read that two lines of my applicationContext wasn't adapted for the solution I try to implement:


<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>


So I removed it. I face now to another error:


Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
org.springframework.transaction.TransactionSystemE xception: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
at org.springframework.orm.hibernate3.HibernateTransa ctionManager.doCommit(HibernateTransactionManager. java:600)
at org.springframework.transaction.support.AbstractPl atformTransactionManager.processCommit(AbstractPla tformTransactionManager.java:709)
at org.springframework.transaction.support.AbstractPl atformTransactionManager.commit(AbstractPlatformTr ansactionManager.java:678)
at org.springframework.transaction.interceptor.Transa ctionAspectSupport.commitTransactionAfterReturning (TransactionAspectSupport.java:319)
at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:116)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :171)
at org.springframework.aop.interceptor.ExposeInvocati onInterceptor.invoke(ExposeInvocationInterceptor.j ava:89)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :171)
at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:204)


Already discussed in others posts, I understood that I make a mix between hibernate transaction management and Spring transaction management, but I don't really understand what part of my configuration do that.

Somebody can give me more details about that?

Thanks in advance

Julien

jbarbede
Mar 12th, 2008, 10:54 AM
I've not found the solution for the moment. I consult the Spring documentation but I think I miss something.

Somebody to help me??

saky
Feb 11th, 2010, 11:29 AM
Is there anyone who has solved this would like to share ?

zhamdi
May 6th, 2010, 11:39 AM
Hi, I have the same problem as you,

you are trying to either commit or rollablck an already committed transaction