日期:2014-05-18  浏览次数:20678 次

为什么分层结构,Spring 事务就不起作用呢,如果不分层就起作用。
public class EntryServlet extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletExceptionIOException{
。。。
Tool.doExecute(json);
}
}




public class Tool {

public static String doExecute(String json) throws Exception {
.....
str=service1.save(newJson);//往表1插入数据

str=service2.save(newJson);//往表2插入数据

}
}

针对EntryServlet的doPost进行了事务配置

针对Tool的doExecute进行了事务配置

针对save进行了事务配置

以上是分层实现的,为什么事务不起作用呢。


如果我不分层实现,比如按照下面这种方式,对 entry进行事务配置,就起作用

public class classEnery{

public entry(){
this.doexece();
}

 public void doexece(){
save1();//往表1插入数据
save2();//往表2插入数据
 } 
 } 
------解决方案--------------------
severlet不能注入bean的,你在severlet里,重写init方法


public void init(ServletConfig servletConfig) throws ServletException {

super.init(servletConfig);
bean=WebApplicationContextUtils
.getRequiredWebApplicationContext(getServletContext()).getBean(
"bean");


获取bean后,在调用方法看看。
------解决方案--------------------
把Tool类里的doExecute方法改成非static的,然后实例化一个Tool对象,再用Tool对象调用doExecute,试一下看看行不行,我觉得楼主的分成和不分层,就差了这个static问题,其余没区别。
------解决方案--------------------
我发现你的事务弄的很乱啊,你制作servlet上配事务就行,另外两个地方去掉,你save方法上怎么也有事务