用动态代理实现事务,不能回滚,求解!
我的servlet:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException,
IOException {
request.setCharacterEncoding("gbk");
String username=request.getParameter("username");
String password=request.getParameter("password");
User user=new User();
user.setName(username);
user.setPass(password);
String title=request.getParameter("title");
String content=request.getParameter("content");
Message message=new Message();
message.setTitle(title);
message.setContent(content);
message.setUid(1);
UserDao userDao=new UserDao();
userDao.setUser(user);
MessageDao messageDao=new MessageDao();
messageDao.setMessage(message);
ConcreteTest concrete=new ConcreteTest();
InvocationHandler ds=new Handler(concrete);
Class cls=concrete.getClass();
Class c=Proxy.getProxyClass(cls.getClassLoader(),cls.getInterfaces());
try {
Constructor ct=c.getConstructor(new Class[]{InvocationHandler.class});
TestInterface testInterface=(TestInterface) ct.newInstance(new Object[]{ds});
if(!testInterface.StoreObject(messageDao, userDao))
{
request.setAttribute("message","失败了");
request.getRequestDispatcher("/fail.jsp").forward(request,response);
return;
}
} catch (
SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (
IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (
InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (
IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (
InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (
SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我的接口类:
public interface TestInterface {
public abstract boolean StoreObject(MessageDao messageDao,UserDao userDao) throws SQLException;
}
我的实现类:
public class ConcreteTest implements TestInterface {
public boolean StoreObject(MessageDao messageDao,UserDao userDao) throws SQLException {
boolean b=false;
try {
if (userDao.saveUser(conn)) {
messageDao.saveMessage(conn);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw e;
}
b=true;
return b;
}
handler类:
public class Handler implements InvocationHandler{
private Object sub;
public Handler(Object sub)
{
this.sub=sub;
}
public Object invoke(Object obj, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
Connection conn=JdbcUtil.getCon