TransactionProxyFactoryBean总结并测试事务异常
2008年09月11日 星期四 17:21
-----------------------------------------------------------------------------------------------------------
TransactionProxyFactoryBean总结并测试事务异常
-----------------------------------------------------------------------------------------------------------
不处于事务控制下
无论是否发生导常
都 不提交记录
-----------------------------------------------------------------------------------------------------------
处于事务控制下
发生导常
不提交记录
-----------------------------------------------------------------------------------------------------------
SQL
-----------------------------------------------------------------------------------------------------------
DROP TABLE IF EXISTS `test`.`users`;
CREATE TABLE `test`.`users` (
`id` varchar(32) NOT NULL,
`name` varchar(40) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-----------------------------------------------------------------------------------------------------------
用TransactionProxyFactoryBean测试事务异常
TransactionProxyFactoryBean总结
配置文件最好分开
-----------------------------------------------------------------------------------------------------------
先添加Spring Capabilities
再添加Hibernate Capabilities
-----------------------------------------------------------------------------------------------------------
Users.hbm.xml
-----------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class table="users" catalog="test">
<id type="java.lang.String">
<column length="32" />
<generator ></generator>
</id>
<property type="java.lang.String">
<column length="40" />
</property>
</class>
</hibernate-mapping>
-----------------------------------------------------------------------------------------------------------
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.0.xsd">
<bean
>
<property
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean >
<property >
<ref bean="localSessionFactory" />
</property>
</bean>
<!-- 开发者添加 -->
<!-- 定义事务管理器,使用适用于Hibernte的事务管理器-->
<bean
>
<property >
<ref bean="localSessionFactory" />
</property>
</bean>
<!-- service manager Template-->
<bean abstract="true"
>
<property ref="transactionManager" />
<property >
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="disable*">PROPAGATION_REQUIRED</prop>
<prop key="enable*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="set*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean parent="txProxyTemplate">
<property >
<bean >
<property ref="UsersDAO" />
</bean>
</property>
</bean>
<!-- 没有事务
<bean >
<property ref="UsersDAO" />
</bean>
-->
<!-- 开发者添加 -->
</beans>
-----------------------------------------------------------------------------------------------------------
hibernate.cfg.xml
-----------------------------------------------------------------------------------------------------------
<?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/test
</property>
<property >
org.hibernate.dialect.MySQLDialect
</property>
<property >
local_mysql
</property>
<property >
com.mysql.jdbc.Driver
</property>
<!-- 开发者添加 -->
<property >true</property>
<!-- 开发者添加 -->
<mapping resource="dao/Users.hbm.xml" />
</session-factory>
</hibernate-configuration>
-----------------------------------------------------------------------------------------------------------
package service;
public class ServiceFactory extends HibernateDaoSupport {
public static ApplicationContext ctx;
public static ApplicationContext getApplicationContext() {
if (ctx == null) {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
return ctx;
}
// public static BeanFactory factory;
protected BeanFactory factory;
public BeanFactory getBeanFactory() {
if (factory == null) {
// ApplicationContext context = new ClassPathXmlApplicationContext(
// new String[] { "applicationContext.xml" });
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
factory = (BeanFactory) context;
}
return factory;
}
}
-----------------------------------------------------------------------------------------------------------
package service;
import dao.Users;
public interface UsersService {
public void save(Users c);
}
-----------------------------------------------------------------------------------------------------------
package service;
import dao.Users;
import dao.UsersDAO;
public class UsersServiceImpl implements UsersService {
public void save(Users c) {
usersdao.save(c);
// 下面两行代码没有实际意义,仅仅为了引发数据库异常
// DataSource ds = null;
// DataSourceUtils.getConnection(ds);
}
private UsersDAO usersdao;
public UsersDAO getUsersdao() {
return usersdao;
}
public void setUsersdao(UsersDAO usersdao) {
System.out.println("注入 dao");
this.usersdao = usersdao;
}
}
-----------------------------------------------------------------------------------------------------------
package test;
import service.ServiceFactory;
import service.UsersService;
import dao.Users;
public class Demo {
public static void main(String[] args) {
ServiceFactory serviceFactory = new ServiceFactory();
UsersService service = (UsersService) serviceFactory.getBeanFactory()
.getBean("UsersServiceImpl");
Users c = new Users();
c.setName("xiong0001");
service.save(c);
System.out.println("END");
}
}
-----------------------------------------------------------------------------------------------------------
测试成功
-----------------------------------------------------------------------------------------------------------
action-servlet.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.0.xsd">
<bean
>
<property ref="UsersServiceImpl" />
</bean>
</beans>
-----------------------------------------------------------------------------------------------------------
struts-config.xml
-----------------------------------------------------------------------------------------------------------
<!-- 开发者添加 -->
<controller
processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/classes/applicationContext.xml,/WEB-INF/classes/action-servlet.xml" />
</plug-in>
<!-- 开发者添加 -->
-----------------------------------------------------------------------------------------------------------
user.jsp
-----------------------------------------------------------------------------------------------------------
<html:form action="/user.do?method=save" method="post">
-----------------------------------------------------------------------------------------------------------
package com.yourcompany.struts.action;
public class UserAction extends DispatchAction {
private UsersService userService;
public UsersService getUserService() {
return userService;
}
public void setUserService(UsersService userService) {
System.out.println("注入 action");
this.userService = userService;
}
public ActionForward save(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm = (UserForm) form;
Users c = new Users();
c.setName(userForm.getName());
userService.save(c);
System.out.println("END");
return null;
}
}
-----------------------------------------------------------------------------------------------------------
TransactionProxyFactoryBean总结并测试事务异常
-----------------------------------------------------------------------------------------------------------
不处于事务控制下
无论是否发生导常
都 不提交记录
-----------------------------------------------------------------------------------------------------------
处于事务控制下
发生导常
不提交记录
-----------------------------------------------------------------------------------------------------------
SQL
-----------------------------------------------------------------------------------------------------------
DROP TABLE IF EXISTS `test`.`users`;
CREATE TABLE `test`.`users` (
`id` varchar(32) NOT NULL,
`name` varchar(40) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-----------------------------------------------------------------------------------------------------------
用TransactionProxyFactoryBean测试事务异常
TransactionProxyFactoryBean总结
配置文件最好分开
-----------------------------------------------------------------------------------------------------------
先添加Spring Capabilities
再添加Hibernate Capabilities
-----------------------------------------------------------------------------------------------------------
Users.hbm.xml
-----------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class table="users" catalog="test">
<id type="java.lang.String">
<column length="32" />
<generator ></generator>
</id>
<property type="java.lang.String">
<column length="40" />
</property>
</class>
</hibernate-mapping>
-----------------------------------------------------------------------------------------------------------
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.0.xsd">
<bean
>
<property
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean >
<property >
<ref bean="localSessionFactory" />
</property>
</bean>
<!-- 开发者添加 -->
<!-- 定义事务管理器,使用适用于Hibernte的事务管理器-->
<bean
>
<property >
<ref bean="localSessionFactory" />
</property>
</bean>
<!-- service manager Template-->
<bean abstract="true"
>
<property ref="transactionManager" />
<property >
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="disable*">PROPAGATION_REQUIRED</prop>
<prop key="enable*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="set*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean parent="txProxyTemplate">
<property >
<bean >
<property ref="UsersDAO" />
</bean>
</property>
</bean>
<!-- 没有事务
<bean >
<property ref="UsersDAO" />
</bean>
-->
<!-- 开发者添加 -->
</beans>
-----------------------------------------------------------------------------------------------------------
hibernate.cfg.xml
-----------------------------------------------------------------------------------------------------------
<?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/test
</property>
<property >
org.hibernate.dialect.MySQLDialect
</property>
<property >
local_mysql
</property>
<property >
com.mysql.jdbc.Driver
</property>
<!-- 开发者添加 -->
<property >true</property>
<!-- 开发者添加 -->
<mapping resource="dao/Users.hbm.xml" />
</session-factory>
</hibernate-configuration>
-----------------------------------------------------------------------------------------------------------
package service;
public class ServiceFactory extends HibernateDaoSupport {
public static ApplicationContext ctx;
public static ApplicationContext getApplicationContext() {
if (ctx == null) {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
return ctx;
}
// public static BeanFactory factory;
protected BeanFactory factory;
public BeanFactory getBeanFactory() {
if (factory == null) {
// ApplicationContext context = new ClassPathXmlApplicationContext(
// new String[] { "applicationContext.xml" });
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
factory = (BeanFactory) context;
}
return factory;
}
}
-----------------------------------------------------------------------------------------------------------
package service;
import dao.Users;
public interface UsersService {
public void save(Users c);
}
-----------------------------------------------------------------------------------------------------------
package service;
import dao.Users;
import dao.UsersDAO;
public class UsersServiceImpl implements UsersService {
public void save(Users c) {
usersdao.save(c);
// 下面两行代码没有实际意义,仅仅为了引发数据库异常
// DataSource ds = null;
// DataSourceUtils.getConnection(ds);
}
private UsersDAO usersdao;
public UsersDAO getUsersdao() {
return usersdao;
}
public void setUsersdao(UsersDAO usersdao) {
System.out.println("注入 dao");
this.usersdao = usersdao;
}
}
-----------------------------------------------------------------------------------------------------------
package test;
import service.ServiceFactory;
import service.UsersService;
import dao.Users;
public class Demo {
public static void main(String[] args) {
ServiceFactory serviceFactory = new ServiceFactory();
UsersService service = (UsersService) serviceFactory.getBeanFactory()
.getBean("UsersServiceImpl");
Users c = new Users();
c.setName("xiong0001");
service.save(c);
System.out.println("END");
}
}
-----------------------------------------------------------------------------------------------------------
测试成功
-----------------------------------------------------------------------------------------------------------
action-servlet.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.0.xsd">
<bean
>
<property ref="UsersServiceImpl" />
</bean>
</beans>
-----------------------------------------------------------------------------------------------------------
struts-config.xml
-----------------------------------------------------------------------------------------------------------
<!-- 开发者添加 -->
<controller
processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/classes/applicationContext.xml,/WEB-INF/classes/action-servlet.xml" />
</plug-in>
<!-- 开发者添加 -->
-----------------------------------------------------------------------------------------------------------
user.jsp
-----------------------------------------------------------------------------------------------------------
<html:form action="/user.do?method=save" method="post">
-----------------------------------------------------------------------------------------------------------
package com.yourcompany.struts.action;
public class UserAction extends DispatchAction {
private UsersService userService;
public UsersService getUserService() {
return userService;
}
public void setUserService(UsersService userService) {
System.out.println("注入 action");
this.userService = userService;
}
public ActionForward save(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm = (UserForm) form;
Users c = new Users();
c.setName(userForm.getName());
userService.save(c);
System.out.println("END");
return null;
}
}
-----------------------------------------------------------------------------------------------------------
本篇日志被作者设置为禁止发表新评论
©2008 Baidu
引文来源 TransactionProxyFactoryBean总结并测试事务异常_熊熊之家