什么是声明式事务?Java中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了什么是声明式事务?Java中相关的知识,希望对你有一定的参考价值。
参考技术A 你说的是集成spring和hibernate所使用的声明式事务吧,就是在没用spring集成前,与数据库交互的增删改操作都需要开启事务再提交对吧,现在这些事情全交给spring做了,你只需要在applicationContext文件中导入相应命名空间再配置一下就可以了。预知详情,百度一下你就知道。。马士兵Spring-声明式事务管理-XML
1.com.cy.model中User.java 和 Log.java 实体 和上一节一样;
2.DAO的实现类com.cy.dao.impl中的UserDAOImpl、LogDAOImpl.java和上一节一样;
3.UserService.java:
package com.cy.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import com.cy.dao.LogDAO; import com.cy.dao.UserDAO; import com.cy.model.Log; import com.cy.model.User; @Component("userService") public class UserService { @Resource private UserDAO userDAO; @Resource private LogDAO logDAO; public void init() { System.out.println("init"); } public User getUser(int id) { return null; } /** * 插入一个用户的同时,记录一条日志 */ public void add(User user) { userDAO.save(user); Log log = new Log(); log.setMsg("a user saved!"); logDAO.save(log); } public UserDAO getUserDAO() { return userDAO; } public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; } public void destroy() { System.out.println("destroy"); } }
4.beans.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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.cy"/> <!-- 如果下面使用到了占位符的方式,请在locations下面去找 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 初始化SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>com.cy.model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <aop:config> <aop:pointcut id="bussinessService" expression="execution(public * com.cy.service..*.*(..))" /> <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="getUser" read-only="true" /> <tx:method name="add*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> </beans>
5.测试代码:
package com.cy.service; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cy.model.User; public class UserServiceTest { @Test public void testAdd() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService)ctx.getBean("userService"); System.out.println(service.getClass()); service.add(new User()); ctx.destroy(); } }
1)正常情况下测试,没有异常抛出,ok,user和log都插入;
2)在LogDAOImpl中的save方法中手动抛出一个异常,插入失败;user没有插入成功,已经回滚了。
以上是关于什么是声明式事务?Java中的主要内容,如果未能解决你的问题,请参考以下文章