BeanNameAutoProxyCreator总结并测试事务异常

BeanNameAutoProxyCreator总结并测试事务异常
2008年04月25日 星期五 15:36
-----------------------------------------------------------------------------------------------------------
不处于事务控制下
无论是否发生导常
都 不提交记录

-----------------------------------------------------------------------------------------------------------

处于事务控制下
发生导常
不提交记录
-----------------------------------------------------------------------------------------------------------

配置文件最好分开

<prop key="hibernate.show_sql">true</prop>
-----------------------------------------------------------------------------------------------------------
drop TABLE sampledb.orders;
CREATE TABLE sampledb.orders(
`ID` BIGINT NOT NULL,
`ORDER_NUMBER` VARCHAR(15),
`CUSTOMER_ID` BIGINT,
PRIMARY KEY(`ID`)
)
ENGINE = InnoDB;

drop TABLE `sampledb`.`customers`;
CREATE TABLE `sampledb`.`customers` (
`ID` BIGINT NOT NULL,
`NAME` VARCHAR(45),
PRIMARY KEY(`ID`)
)
ENGINE = InnoDB;


alter table sampledb.orders
add constraint constraint_constraint_fk
foreign key (CUSTOMER_ID)
references sampledb.customers(ID);

-----------------------------------------------------------------------------------------------------------

用BeanNameAutoProxyCreator测试事务异常
BeanNameAutoProxyCreator总结
配置文件分开
-----------------------------------------------------------------------------------------------------------

先添加Spring Capabilities
再添加Hibernate Capabilities


-----------------------------------------------------------------------------------------------------------


package service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServiceFactory {
public static ApplicationContext ctx;
public static ApplicationContext getApplicationContext() {
if (ctx == null) {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
return ctx;
}
}

-----------------------------------------------------------------------------------------------------------

package service;
import db.Customers;
public interface CustomersService {
public void save(Customers c);
}

-----------------------------------------------------------------------------------------------------------

package service;
import javax.sql.DataSource;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;
import db.Customers;
import db.CustomersDAO;
public class CustomersServiceImpl extends HibernateDaoSupport implements
CustomersService {
private TransactionTemplate transactionTemplate;
public void setTransactionManager(
PlatformTransactionManager transactionManager) {
this.transactionTemplate = new TransactionTemplate(transactionManager);
}
public void save(Customers c) {
CustomersDAO dao = (CustomersDAO) ServiceFactory
.getApplicationContext().getBean("CustomersDAO");
dao.save(c);
// 下面两行代码没有实际意义,仅仅为了引发数据库异常
//DataSource ds = null;
//DataSourceUtils.getConnection(ds);
}
}
-----------------------------------------------------------------------------------------------------------

package test;
import service.CustomersService;
import service.ServiceFactory;
import db.Customers;
public class Demo {
public static void main(String[] args) {
CustomersService service = (CustomersService) ServiceFactory
.getApplicationContext().getBean("CustomersServiceImpl");
Customers c = new Customers();
c.setId(1l);
c.setName("xiong");
service.save(c);
System.out.println("END");
}
}

-----------------------------------------------------------------------------------------------------------

<?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.0.xsd">


<bean
>
<property
value="file:src/hibernate.cfg.xml">
</property>
</bean>
<bean >
<property >
<ref bean="localSessionFactory" />
</property>
</bean>
<bean >
<property >
<ref bean="localSessionFactory" />
</property>
</bean>








<bean >
<property >
<ref bean="localSessionFactory" />
</property>
</bean>




<!-- 定义事务管理器,使用适用于Hibernte的事务管理器-->

<bean
>
<property >
<ref bean="localSessionFactory" />
</property>
</bean>







<!-- 配置事务拦截器-->
<bean
>
<!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
<property ref="transactionManager" />
<property >
<!-- 下面定义事务传播属性-->
<props>
<!-- <prop key="save">PROPAGATION_REQUIRED</prop> -->
<prop key="save*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 定义BeanNameAutoProxyCreator,该bean是个bean后处理器,无需被引用,因此没有id属性
这个bean后处理器,根据事务拦截器为目标bean自动创建事务代理
<bean >
指定对满足哪些bean name的bean自动生成业务代理 -->
<bean
>
<property >
<!-- 下面是所有需要自动创建事务代理的bean-->
<list>
<!-- <value>CustomersServiceImpl</value>-->
<value>CustomersServiceImpl</value>

</list>
<!-- 此处可增加其他需要自动创建事务代理的bean-->
</property>
<!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
<property >
<list>
<value>transactionInterceptor</value>
<!-- 此处可增加其他新的Interceptor -->
</list>
</property>
</bean>




</beans>
-----------------------------------------------------------------------------------------------------------

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property >root</property>
<property >
jdbc:mysql://localhost:3306/sampledb
</property>
<property >
org.hibernate.dialect.MySQLDialect
</property>
<property >
local_mysql
</property>
<property >
com.mysql.jdbc.Driver
</property>
<property >true</property>
<mapping resource="db/Orders.hbm.xml" />
<mapping resource="db/Customers.hbm.xml" />

</session-factory>

</hibernate-configuration>

-----------------------------------------------------------------------------------------------------------


本篇日志被作者设置为禁止发表新评论


©2008 Baidu



引文来源 BeanNameAutoProxyCreator总结并测试事务异常_熊熊之家
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值