import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
public class TransactionHandler implements InvocationHandler {
private Object targetObject;
public Object createProxyObject(Object targetObject) {
this.targetObject = targetObject;
return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(),
targetObject.getClass().getInterfaces(),
this);
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Connection conn = null;
Object retValue = null;
try {
conn = ConnectionManager.getConnection();
if (method.getName().startsWith("add") ||
method.getName().startsWith("delete") ||
method.getName().startsWith("modify")) {
System.out.println("begin transaction");
//开启事务
conn.setAutoCommit(false);
}
//调用目标对象上的业务逻辑方法
retValue = method.invoke(this.targetObject, args);
if (!conn.getAutoCommit()) {
System.out.println("commit transaction");
//提交事务
conn.commit();
}
// }catch(AppException e) {
// if (!conn.getAutoCommit()) {
// System.out.println("rollback transaction");
// //回滚事务
// conn.rollback();
// }
// throw e;
}catch(Exception e) {
e.printStackTrace();
if (!conn.getAutoCommit()) {
System.out.println("rollback transaction");
//回滚事务
conn.rollback();
}
if (e instanceof InvocationTargetException) {
InvocationTargetException ite = (InvocationTargetException)e;
throw ite.getTargetException();
}
throw new AppException("操作错误!");
}finally {
conn.setAutoCommit(true);
ConnectionManager.closeConnection();
}
return retValue;
}
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
public class TransactionHandler implements InvocationHandler {
private Object targetObject;
public Object createProxyObject(Object targetObject) {
this.targetObject = targetObject;
return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(),
targetObject.getClass().getInterfaces(),
this);
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Connection conn = null;
Object retValue = null;
try {
conn = ConnectionManager.getConnection();
if (method.getName().startsWith("add") ||
method.getName().startsWith("delete") ||
method.getName().startsWith("modify")) {
System.out.println("begin transaction");
//开启事务
conn.setAutoCommit(false);
}
//调用目标对象上的业务逻辑方法
retValue = method.invoke(this.targetObject, args);
if (!conn.getAutoCommit()) {
System.out.println("commit transaction");
//提交事务
conn.commit();
}
// }catch(AppException e) {
// if (!conn.getAutoCommit()) {
// System.out.println("rollback transaction");
// //回滚事务
// conn.rollback();
// }
// throw e;
}catch(Exception e) {
e.printStackTrace();
if (!conn.getAutoCommit()) {
System.out.println("rollback transaction");
//回滚事务
conn.rollback();
}
if (e instanceof InvocationTargetException) {
InvocationTargetException ite = (InvocationTargetException)e;
throw ite.getTargetException();
}
throw new AppException("操作错误!");
}finally {
conn.setAutoCommit(true);
ConnectionManager.closeConnection();
}
return retValue;
}
}