Spring实现iBATIS事务回滚
2012-03-28 22:45
330 查看
配置如下:
Spring实现iBATIS事务回滚之Web.xml
﹤?xml version="1.0" encoding="UTF-8"?﹥
﹤web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"﹥
﹤context-param﹥
﹤param-name﹥contextConfigLocation﹤/param-name﹥
﹤param-value﹥classpath:applicationContext.xml﹤/param-value﹥
﹤/context-param﹥
﹤listener﹥
﹤listener-class﹥
org.springframework.web.context.ContextLoaderListener
﹤/listener-class﹥
﹤/listener﹥
﹤servlet﹥
﹤servlet-name﹥AppConfigServlet﹤/servlet-name﹥
﹤servlet-class﹥com.test.ConfigServlet﹤/servlet-class﹥
﹤load-on-startup﹥2﹤/load-on-startup﹥
﹤/servlet﹥
﹤welcome-file-list﹥
﹤welcome-file﹥index.jsp﹤/welcome-file﹥
﹤/welcome-file-list﹥
﹤/web-app﹥
Spring实现iBATIS事务回滚之ApplicationContext.xml
﹤bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"﹥
﹤property name="dataSource" ref="dataSource" /﹥
﹤/bean﹥
﹤bean id="businessTarget"
class="com.test.BusinessInterfaceImpl" /﹥
﹤bean id="businessBean"
class="org.springframework.aop.framework.ProxyFactoryBean"﹥
﹤property name="singleton"﹥
﹤value﹥true﹤/value﹥
﹤/property﹥
﹤property name="proxyInterfaces"﹥
﹤value﹥com.test.BusinessInterface﹤/value﹥
﹤/property﹥
﹤property name="interceptorNames"﹥
﹤list﹥
﹤value﹥transactionInterceptor﹤/value﹥
﹤value﹥businessTarget﹤/value﹥
﹤/list﹥
﹤/property﹥
﹤property name="proxyTargetClass"﹥﹤value﹥true﹤/value﹥﹤/property﹥
﹤/bean﹥
﹤bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor"﹥
﹤property name="transactionManager"﹥
﹤ref local="transactionManager" /﹥
﹤/property﹥
﹤property name="transactionAttributes"﹥
﹤props﹥
﹤prop key="*"﹥PROPAGATION_REQUIRED,-Exception﹤/prop﹥
﹤/props﹥
﹤/property﹥
﹤/bean﹥
﹤bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean"﹥
﹤property name="jndiName"﹥
﹤value﹥gissys﹤/value﹥
﹤/property﹥
﹤/bean﹥
﹤bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"﹥
﹤property name="configLocation"﹥
﹤value﹥
classpath:com/emaptech/persistence/SqlMapConfig.xml
﹤/value﹥
﹤/property﹥
﹤property name="dataSource" ref="dataSource"﹥﹤/property﹥
﹤/bean﹥
Spring实现iBATIS事务回滚之Ibatis.xml
﹤insert id="testInsert" parameterClass="string"﹥
insert into corebase.cc select 6,#value# from dual
﹤/insert﹥
﹤insert id="testInsertWrong" parameterClass="string"﹥
insert into corebase.cc select 6,#value#,7 from dual
﹤/insert﹥
Spring实现iBATIS事务回滚之ConfigServlet.java
package com.test;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class ConfigServlet extends HttpServlet {
/**
* UID
*/
private static final long serialVersionUID = 5118794568550751611L;
public void init() throws ServletException {
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
BaseService.getInstance().init(ctx);
super.init();
}
}
Spring实现iBATIS事务回滚之BaseService.java
package com.test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.ClassPathResource;
import com.ibatis.sqlmap.client.SqlMapClient;
public class BaseService implements ApplicationContextAware {
private static ApplicationContext ctx;
private static SqlMapClient sqlMapClient;
private static SqlMapClient sqlMapClientProxy;
private static BusinessInterface businessBean;
private static BaseService instance = new BaseService();
/**
* @return 返回 静态类
*/
public static BaseService getInstance(){
return instance;
}
/**
* @return 返回 初始化BaseService,存于内存
*/
public void init(ApplicationContext ctx) {
setApplicationContext(ctx);
setSqlMapClient(ctx);
setBusinessBean(ctx);
// setSqlMapClientProxy(ctx);
System.out.println(" INFO - 初始化baseservice成功");
}
public void setSqlMapClient(ApplicationContext ctx){
sqlMapClient = (SqlMapClient) ctx.getBean("sqlMapClient");
}
/**
* @return 返回 sqlMapClient。
*/
public SqlMapClient getInstanceSqlMapClient() {
return this.sqlMapClient;
}
/**
* 通过spring注入ApplicationContext
*
* @param ApplicationContext
* @return null
*/
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
ctx = arg0;
}
public Object getBean(String beanName) {
return ctx.getBean(beanName);
}
/**
* @return 返回 sqlMapClient。
*/
public SqlMapClient getSqlMapClient() {
return (SqlMapClient) ctx.getBean("sqlMapClient");
}
public void setSqlMapClientProxy(ApplicationContext ctx) {
sqlMapClientProxy= (SqlMapClient) ctx.getBean("sqlMapClientProxy");
}
public SqlMapClient getSqlMapClientProxy() {
return sqlMapClientProxy;
}
public static BusinessInterface getBusinessBean() {
return businessBean;
}
public static void setBusinessBean(ApplicationContext ctx) {
businessBean = (BusinessInterface) ctx.getBean("businessBean");
}
}
Spring实现iBATIS事务回滚之Web.xml
﹤?xml version="1.0" encoding="UTF-8"?﹥
﹤web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"﹥
﹤context-param﹥
﹤param-name﹥contextConfigLocation﹤/param-name﹥
﹤param-value﹥classpath:applicationContext.xml﹤/param-value﹥
﹤/context-param﹥
﹤listener﹥
﹤listener-class﹥
org.springframework.web.context.ContextLoaderListener
﹤/listener-class﹥
﹤/listener﹥
﹤servlet﹥
﹤servlet-name﹥AppConfigServlet﹤/servlet-name﹥
﹤servlet-class﹥com.test.ConfigServlet﹤/servlet-class﹥
﹤load-on-startup﹥2﹤/load-on-startup﹥
﹤/servlet﹥
﹤welcome-file-list﹥
﹤welcome-file﹥index.jsp﹤/welcome-file﹥
﹤/welcome-file-list﹥
﹤/web-app﹥
Spring实现iBATIS事务回滚之ApplicationContext.xml
﹤bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"﹥
﹤property name="dataSource" ref="dataSource" /﹥
﹤/bean﹥
﹤bean id="businessTarget"
class="com.test.BusinessInterfaceImpl" /﹥
﹤bean id="businessBean"
class="org.springframework.aop.framework.ProxyFactoryBean"﹥
﹤property name="singleton"﹥
﹤value﹥true﹤/value﹥
﹤/property﹥
﹤property name="proxyInterfaces"﹥
﹤value﹥com.test.BusinessInterface﹤/value﹥
﹤/property﹥
﹤property name="interceptorNames"﹥
﹤list﹥
﹤value﹥transactionInterceptor﹤/value﹥
﹤value﹥businessTarget﹤/value﹥
﹤/list﹥
﹤/property﹥
﹤property name="proxyTargetClass"﹥﹤value﹥true﹤/value﹥﹤/property﹥
﹤/bean﹥
﹤bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor"﹥
﹤property name="transactionManager"﹥
﹤ref local="transactionManager" /﹥
﹤/property﹥
﹤property name="transactionAttributes"﹥
﹤props﹥
﹤prop key="*"﹥PROPAGATION_REQUIRED,-Exception﹤/prop﹥
﹤/props﹥
﹤/property﹥
﹤/bean﹥
﹤bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean"﹥
﹤property name="jndiName"﹥
﹤value﹥gissys﹤/value﹥
﹤/property﹥
﹤/bean﹥
﹤bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"﹥
﹤property name="configLocation"﹥
﹤value﹥
classpath:com/emaptech/persistence/SqlMapConfig.xml
﹤/value﹥
﹤/property﹥
﹤property name="dataSource" ref="dataSource"﹥﹤/property﹥
﹤/bean﹥
Spring实现iBATIS事务回滚之Ibatis.xml
﹤insert id="testInsert" parameterClass="string"﹥
insert into corebase.cc select 6,#value# from dual
﹤/insert﹥
﹤insert id="testInsertWrong" parameterClass="string"﹥
insert into corebase.cc select 6,#value#,7 from dual
﹤/insert﹥
Spring实现iBATIS事务回滚之ConfigServlet.java
package com.test;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class ConfigServlet extends HttpServlet {
/**
* UID
*/
private static final long serialVersionUID = 5118794568550751611L;
public void init() throws ServletException {
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
BaseService.getInstance().init(ctx);
super.init();
}
}
Spring实现iBATIS事务回滚之BaseService.java
package com.test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.ClassPathResource;
import com.ibatis.sqlmap.client.SqlMapClient;
public class BaseService implements ApplicationContextAware {
private static ApplicationContext ctx;
private static SqlMapClient sqlMapClient;
private static SqlMapClient sqlMapClientProxy;
private static BusinessInterface businessBean;
private static BaseService instance = new BaseService();
/**
* @return 返回 静态类
*/
public static BaseService getInstance(){
return instance;
}
/**
* @return 返回 初始化BaseService,存于内存
*/
public void init(ApplicationContext ctx) {
setApplicationContext(ctx);
setSqlMapClient(ctx);
setBusinessBean(ctx);
// setSqlMapClientProxy(ctx);
System.out.println(" INFO - 初始化baseservice成功");
}
public void setSqlMapClient(ApplicationContext ctx){
sqlMapClient = (SqlMapClient) ctx.getBean("sqlMapClient");
}
/**
* @return 返回 sqlMapClient。
*/
public SqlMapClient getInstanceSqlMapClient() {
return this.sqlMapClient;
}
/**
* 通过spring注入ApplicationContext
*
* @param ApplicationContext
* @return null
*/
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
ctx = arg0;
}
public Object getBean(String beanName) {
return ctx.getBean(beanName);
}
/**
* @return 返回 sqlMapClient。
*/
public SqlMapClient getSqlMapClient() {
return (SqlMapClient) ctx.getBean("sqlMapClient");
}
public void setSqlMapClientProxy(ApplicationContext ctx) {
sqlMapClientProxy= (SqlMapClient) ctx.getBean("sqlMapClientProxy");
}
public SqlMapClient getSqlMapClientProxy() {
return sqlMapClientProxy;
}
public static BusinessInterface getBusinessBean() {
return businessBean;
}
public static void setBusinessBean(ApplicationContext ctx) {
businessBean = (BusinessInterface) ctx.getBean("businessBean");
}
}
相关文章推荐
- Spring实现iBATIS事务回滚~
- Spring实现iBATIS事务回滚
- Spring+iBatis+Atomikos实现JTA事务
- spring2.5.6+ibatis2.3.4实现事务配置方法
- spring代理iBATIS时事务不能回滚
- Spring+iBatis+JOTM实现JTA事务
- Spring事务回滚的实现
- Spring整合Ibatis事务实现
- spring+ibatis+Atomikos实现JTA事务
- 实践中遇到Ibatis和spring整合事务不回滚的问题解决
- Spring+iBatis+Atomikos实现JTA事务
- Spring+iBatis+Atomikos实现JTA事务
- Spring+iBatis+JOTM实现JTA事务
- Spring+iBatis+Atomikos实现JTA事务
- 关于Spring的@Transactional注解失效以及事务无法回滚问题
- Spring中事务的其他属性(隔离级别、回滚、只读、过期)
- Spring注解事务提交回滚
- Spring 2.0声明式事务管理让你的事务轻松回滚!
- spring源码分析之——spring 事务管理实现方式
- ThinkPHP实现事务回滚示例代码