标签:回滚 catch city pre mysq nec yacc hang apache
package com.hope.factory;
import com.hope.service.IAccountService;
import com.hope.utils.TransactionManager;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* @author newcityman
* @date 2019/11/22 - 20:33
* 用于创建service的代理对象的工厂
*/
public class BeanFactory {
private IAccountService accountService;
private TransactionManager txManager;
public void setTxManager(TransactionManager txManager) {
this.txManager = txManager;
}
public final void setAccountService(IAccountService accountService) {
this.accountService = accountService;
}
public IAccountService getAccountService() {
return (IAccountService) Proxy.newProxyInstance(accountService.getClass().getClassLoader(), accountService.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object rtValue = null;
try {
// 1、开启事务
txManager.beainTransaction();
// 2、执行方法
rtValue = method.invoke(accountService, args);
// 3、提交事务
txManager.commit();
// 4、返回结果
return rtValue;
} catch (Exception e) {
// 5、回滚事务
txManager.rollback();
e.printStackTrace();
throw new RuntimeException("错误信息如下:");
} finally {
// 6、释放资源
txManager.release();
}
}
});
}
}
<?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.xsd">
<!--配置代理的service-->
<bean id="proxyAccountService" factory-bean="beanFactory" factory-method="getAccountService"/>
<!--配置beanfactory-->
<bean id="beanFactory" class="com.hope.factory.BeanFactory">
<property name="accountService" ref="accountService"></property>
<property name="txManager" ref="txManager"></property>
</bean>
<!--配置service-->
<bean id="accountService" class="com.hope.service.impl.AccountServiceImpl">
<!--注入dao-->
<property name="accountDao" ref="accountDao"/>
</bean>
<!--配置dao-->
<bean id="accountDao" class="com.hope.dao.impl.AccountDaoImpl">
<!--注入runner-->
<property name="runner" ref="runner"/>
<!--注入connectionUtils-->
<property name="connectionUtils" ref="connctionUtils"/>
</bean>
<!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--连接数据库的必备信息-->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/easy"/>
<property name="user" value="root"/>
<property name="password" value="123"/>
</bean>
<bean name="connctionUtils" class="com.hope.utils.ConnectionUtils">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean name="txManager" class="com.hope.utils.TransactionManager">
<property name="connectionUtils" ref="connctionUtils"/>
</bean>
</beans>
标签:回滚 catch city pre mysq nec yacc hang apache
原文地址:https://www.cnblogs.com/newcityboy/p/11914603.html