在grails加入spring事务支持 转
2012-11-15 16:09
417 查看
grails中有个Service支持事务操作,但你如果想用Spring的事务,可以在grails-app/spring/resources.xml中加入spring的事务声明,如下所示:
resource.xml 代码
<?xml version="1.0" encoding="UTF-8"?>
<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.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- 这是我们将要配置并使它具有事务性的Service对象 -->
<bean id="userImpl" class="transaction.UserImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- the transactional advice (i.e. what 'happens'; see the <aop:advisor/> bean below) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- ensure that the above transactional advice runs for any execution of an operation defined by the FooService interface -->
<aop:config proxy-target-class="true">
<aop:pointcut id="fooServiceOperation" expression="execution(* transaction.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
</aop:config>
<!-- don't forget the DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/OA"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<!-- similarly, don't forget the (particular) PlatformTransactionManager -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- other <bean/> definitions here -->
</beans>
UserImpl.groovy 代码
package transaction
import com.jr.nj.hibernate.Event
import org.hibernate.SessionFactory
import org.hibernate.Session
import org.springframework.orm.hibernate3.HibernateTemplate
class UserImpl {
def sessionFactory
def save(params) throws Exception {
def event =new Event()
event.setDate new Date()
event.setTitle 'aaa'
HibernateTemplate ht = new HibernateTemplate(sessionFactory);
ht.save event
def event2 =new Event()
event2.setDate new Date()
event2.setTitle 'bbb'
ht.save event2
throw new RuntimeException("事务测试")
}
}
其中,Event可以是java定义的持久类或者groovy定义的domain。
原文:http://www.iteye.com/topic/133180
resource.xml 代码
<?xml version="1.0" encoding="UTF-8"?>
<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.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- 这是我们将要配置并使它具有事务性的Service对象 -->
<bean id="userImpl" class="transaction.UserImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- the transactional advice (i.e. what 'happens'; see the <aop:advisor/> bean below) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- ensure that the above transactional advice runs for any execution of an operation defined by the FooService interface -->
<aop:config proxy-target-class="true">
<aop:pointcut id="fooServiceOperation" expression="execution(* transaction.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
</aop:config>
<!-- don't forget the DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/OA"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<!-- similarly, don't forget the (particular) PlatformTransactionManager -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- other <bean/> definitions here -->
</beans>
UserImpl.groovy 代码
package transaction
import com.jr.nj.hibernate.Event
import org.hibernate.SessionFactory
import org.hibernate.Session
import org.springframework.orm.hibernate3.HibernateTemplate
class UserImpl {
def sessionFactory
def save(params) throws Exception {
def event =new Event()
event.setDate new Date()
event.setTitle 'aaa'
HibernateTemplate ht = new HibernateTemplate(sessionFactory);
ht.save event
def event2 =new Event()
event2.setDate new Date()
event2.setTitle 'bbb'
ht.save event2
throw new RuntimeException("事务测试")
}
}
其中,Event可以是java定义的持久类或者groovy定义的domain。
原文:http://www.iteye.com/topic/133180
相关文章推荐
- web项目中加入struts2、spring的支持,并整合两者
- SpringBoot整合Mybatis,多数据源,事务,支持java -jar 启动.
- Spring对事务管理的支持的发展历程--转
- Spring对事务管理的支持的发展历程(基础篇)
- Spring对事务管理支持的发展历程
- 深入理解Spring Redis的使用 (二)、RedisTemplate事务支持、序列化
- Spring对事务管理的支持的发展历程(基础篇)
- spring2.5整合hibernate3.3的泛型Dao并加入spring jdbc的支持
- 普通web工程加入Spring任务的支持
- 用maven整合struts+spring+hibernate之三 加入框架支持
- Spring 支持的事务传播行为
- spring2.5整合hibernate3.3的泛型Dao并加入spring jdbc的支持
- 【Spring】Spring系列5之Spring支持事务处理
- Spring对事务管理的支持的发展历程(基础篇)
- Spring 事务操作(银行转账案例),使用spring 对jdbc的支持完成对数据库的操作
- Spring中事务管理支持哪几种方式以及每种方式的具体使用方法
- spring项目加入jta分布式事务的实现方式: Atomikos
- [Spring学习笔记 7 ] Spring中的数据库支持 RowMapper,JdbcDaoSupport 和 事务处理Transaction
- spring2.5整合hibernate3.3的泛型Dao并加入spring jdbc的支持
- spring aop事务支持与synchronized的问题