How to use transacions with Spring 2.5 AOP and Hibernate 3.2.5
Hello friends.
The first time when i was training to use spring and hibernate i wanted to avoid to open and close transaction manually in every method that does some transaction.
i.e. (pseudo code)
void createSomeObject(par1, par2, par3) {
open transaccion
create object using the given parameters
persist the object
close transaction
}
So i found that spring helps you to do that.
I will assume that you already created the Java Bean (POJO) and it’s hibernate mapping file. ( SomePOJO.java and SomePOJO.hbm.xml files).
1. Define AOP and Transaction namespace in spring application context configuration file. (Find more about AOP and Transaction Management)
<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" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> ... ... </beans>
2. Define data source to configure the database connection parameters.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost/somedb"/>
<property name="username" value="someuser"/>
<property name="password" value="somepassword"/>
</bean>
3. Define Hibernate SessionFactory.
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>/some/package/model/SomePOJO.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
</value>
</property>
</bean>
4. Define Transaction aspect. (more info)
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice>
5. Define point cuts in your service.
<aop:config> <aop:pointcut id="someServiceMethods" expression="execution(* some.package.service.SomeService.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="someServiceMethods"/> </aop:config>
Spring will begin a transaction each time a method in SomeService is called. This behavior can be changed (see) , I just tried to do the example the most simple as possible.
I will try to prepare some package with source code. just let me know. ![]()
Regards
-Renan
Follow me on twitter
Hola renan muy buen inicio, tienen informaciñon en español que nos puede ayuda? espero puedas tener pronto el ejemplo
Hola gracias por el commentario.
Mientras el articulo en español esta en http://javagratis.net63.net/programacion-java.html
Hola Jorge.
Sabes puedes bajar el codigo fuente de este post y sobre este hacer los cambios.
Como configurar una aplicacion basica con Spring 2.5 con Maven 2
Allo Renan Huanca
Your code was very nice.
But I am with a question, could you put in your apllicationContext.xml auto-proxy?
Thank you