spring、hibernate整合2(事务)
概述:
编程式事务管理
声明式事务管理
?
编程式事务管理
编程式事务提供了TransactionTemplate 模板类,该类可以大大减少事务操作的代码。使用TransactionTemplate 不需要显式地开始事务,甚至不需要显式地提交事务。这些步骤都由模板完成。但出现异常时,应通过TransactionStatus setRollbackOnly 显式回滚事务。
1、applicationContext.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><import resource="DataSource.xml" /><!-- 定义hibernate的SessionFactory --><bean id="sessionFactory"/></property></bean><!-- 配置Hibernate的事务管理器 --><!--使用HibernateTransactionManager类,该类是PlatformTransactionManager接口针对采用Hibernate持久化连接的特定实现。--><bean id="transactionManager"/></property></bean></beans>?
2、DataSource.xml
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 定义数据源 --><bean id="dataSource" name="code"><?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class table="Student" name="com.oracle.Student"><id unsaved-value="0" name="studentid" type="int" column="studentid"><generator /></id><property name="name" column="name" unique="false" not-null="false" type="string" /><property name="sex" column="sex" unique="false" not-null="false" type="string" /><property name="address" column="address" unique="false" not-null="false" type="string" /></class></hibernate-mapping>?
4、Student.java
package com.oracle;public class Student {private int studentid;private String name;private String sex;private String address;public int getStudentid() {return studentid;}public void setStudentid(int studentid) {this.studentid = studentid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}}?
5、入口
import org.hibernate.SessionFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.orm.hibernate3.HibernateTemplate;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.TransactionDefinition;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.support.TransactionCallbackWithoutResult;import org.springframework.transaction.support.TransactionTemplate;import com.oracle.Student;public class Test0 {public static void main(String[] args) {// 编程式事务管理// 因为并未在web 应用中测试,故需要手动创建Spring 的上下文//final FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml");ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");// 获得事务管理器PlatformTransactionManager transactionManager = (PlatformTransactionManager)ctx.getBean("transactionManager");final SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");// 以事务管理器实例为参数,创建TransactionTemplate对象TransactionTemplate tt = new TransactionTemplate(transactionManager);// 设置TransactionTemplate的事务传播属性tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);// 执行TransactionTemplate的execute方法,该方法需要TransactionCallback实例tt.execute(new TransactionCallbackWithoutResult() {// 采用TransactionCallbackWithoutResult匿名内部类的形式执行protected void doInTransactionWithoutResult(TransactionStatus ts) {try {// 以SessionFactory 实例为参数创建HibernateTemplateHibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);Student s0= new Student();s0.setName("KoBeKoBe");s0.setSex("KoBeKoBe");s0.setAddress("KoBeKoBe");// 保存第一个实例hibernateTemplate.save(s0);// 让下面的数据库操作抛出异常即可看出事务效果。前面的操作也// 不会生效Student s1= new Student();s1.setName("NaShNaShNaShNaShNaShNaShNaShNaShNaShNaShNaShNaShNaShNaShNaShNaSh");s1.setSex("NaSh");s1.setAddress("NaSh");// 保存第二个实例,name长度过长,引起异常,可看出前一条记录也不会加入数据库中hibernateTemplate.save(s1);} catch (Exception e) {ts.setRollbackOnly();}}});}}?
6、表
DROP TABLE IF EXISTS `orders`.`student`;CREATE TABLE `orders`.`student` ( `studentid` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(45) NOT NULL, `sex` varchar(45) NOT NULL, `address` varchar(45) NOT NULL, PRIMARY KEY (`studentid`) USING BTREE) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=gbk;?
?
?
?
声明式事务管理
?