为什么分层结构,Spring 事务就不起作用呢,如果不分层就起作用。

myepoch 2014-01-27 11:14:53
public class EntryServlet extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
。。。
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插入数据
}
}
...全文
317 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
你先把service在severlet里获取看看。
myepoch 2014-01-27
  • 打赏
  • 举报
回复
引用 1 楼 fangmingshijie 的回复:
severlet不能注入bean的,你在severlet里,重写init方法


public void init(ServletConfig servletConfig) throws ServletException {

		super.init(servletConfig);
		bean=WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext()).getBean(
						"bean");
获取bean后,在调用方法看看。
你好,但是我的severlet并不需要注入BEAN,BEAN名称我通过json字符串传到Tool类的,Tool类才需要BEAN
  • 打赏
  • 举报
回复
severlet不能注入bean的,你在severlet里,重写init方法


public void init(ServletConfig servletConfig) throws ServletException {

		super.init(servletConfig);
		bean=WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext()).getBean(
						"bean");
获取bean后,在调用方法看看。
myepoch 2014-01-27
  • 打赏
  • 举报
回复
引用 9 楼 fangmingshijie 的回复:
service = (IService)BeanService.getBean("shtg"); 你把这个放在doPost里面是不行的,放在init方法内,service定义成全部变量,在init方法内赋值,你看下severlet的生命周期。
这样是没有问题的,程序是正确的,就是事务不起作用。
myepoch 2014-01-27
  • 打赏
  • 举报
回复
引用 17 楼 LZW190 的回复:
实验也不能这样乱来,你这分明是事务嵌套了
即便不嵌套,也是一样的。配置已经从其他方面证实是没问题的,你可以参考 http://bbs.csdn.net/topics/390701800 这个。这个不存在嵌套问题,但问题依旧,谢谢
雕虫大计 2014-01-27
  • 打赏
  • 举报
回复
实验也不能这样乱来,你这分明是事务嵌套了
myepoch 2014-01-27
  • 打赏
  • 举报
回复
引用 15 楼 LZW190 的回复:
我发现你的事务弄的很乱啊,你制作servlet上配事务就行,另外两个地方去掉,你save方法上怎么也有事务
哥们,那是为了试验。
雕虫大计 2014-01-27
  • 打赏
  • 举报
回复
我发现你的事务弄的很乱啊,你制作servlet上配事务就行,另外两个地方去掉,你save方法上怎么也有事务
myepoch 2014-01-27
  • 打赏
  • 举报
回复
引用 13 楼 LZW190 的回复:
事务你怎么配置的?
配置没有问题,因为通过其他方式已经验证
雕虫大计 2014-01-27
  • 打赏
  • 举报
回复
事务你怎么配置的?
myepoch 2014-01-27
  • 打赏
  • 举报
回复
引用 11 楼 baohuan_love 的回复:
把Tool类里的doExecute方法改成非static的,然后实例化一个Tool对象,再用Tool对象调用doExecute,试一下看看行不行,我觉得楼主的分成和不分层,就差了这个static问题,其余没区别。
和是否静态没有关系,因为即便不是静态,一样不起作用。
  • 打赏
  • 举报
回复
把Tool类里的doExecute方法改成非static的,然后实例化一个Tool对象,再用Tool对象调用doExecute,试一下看看行不行,我觉得楼主的分成和不分层,就差了这个static问题,其余没区别。
myepoch 2014-01-27
  • 打赏
  • 举报
回复
引用 7 楼 fangmingshijie 的回复:
你配置<tx:method 接下来配置的肯定是<aop:config> <aop:pointcut id="xx" expression="execution(* xx.oo.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="xx" /> </aop:config> 那你看你Shtg是否扫描到。
<aop:config> <aop:pointcut id="basePointCut" expression="execution(* com.lgm..*.*(..))" /> <aop:advisor pointcut-ref="basePointCut" advice-ref="txAdvice" /> </aop:config> 我的类都是配置在com.lgm包下的,应该没有问题的 如果我这样做,事务就起作用。 public class EntryServlet extends HttpServlet{ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ IService service = null; String newJson=strJson(content,0); service = (IService)BeanService.getBean("shtg"); str=service.doExecute(newJson);//对应Shtg类 //newJson=strJson(content,1); //service = (IService)BeanService.getBean("jyztUpdate"); //str=service.doExecute(newJson); //对应JyztUpdate 类 } } public class Shtg{ public String doExecute(String jsonStr) throws RuntimeException { baseDao.saveObj(gsh);//注意,此处将保存写在Shtg类的doExecute方法,事务就起作用 String sql= "update GadwRyXx set jyzt = :jyzt,kyzt='Addd' where guid = :guid and kyzt = '1'"; baseDao.update(sql, parameters); } }
  • 打赏
  • 举报
回复
service = (IService)BeanService.getBean("shtg");
你把这个放在doPost里面是不行的,放在init方法内,service定义成全部变量,在init方法内赋值,你看下severlet的生命周期。
myepoch 2014-01-27
  • 打赏
  • 举报
回复
引用 7 楼 fangmingshijie 的回复:
你配置<tx:method 接下来配置的肯定是<aop:config> <aop:pointcut id="xx" expression="execution(* xx.oo.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="xx" /> </aop:config> 那你看你Shtg是否扫描到。
<aop:config> <aop:pointcut id="basePointCut" expression="execution(* com.lgm..*.*(..))" /> <aop:advisor pointcut-ref="basePointCut" advice-ref="txAdvice" /> </aop:config> 我的类都是配置在com.lgm包下的,应该没有问题的
  • 打赏
  • 举报
回复
你配置<tx:method 接下来配置的肯定是<aop:config> <aop:pointcut id="xx" expression="execution(* xx.oo.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="xx" /> </aop:config> 那你看你Shtg是否扫描到。
myepoch 2014-01-27
  • 打赏
  • 举报
回复
引用 5 楼 fangmingshijie 的回复:
spring的事务管理不到severlet
是的,当初为了找不起作用的原因,所以才都配置上的。 但是即便现在我不配置POST的事务,依然不行的啊。 真实的代码 public class EntryServlet extends HttpServlet{ public void init(ServletConfig config) throws ServletException { //System.out.println("DB init 初始化"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost( request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ IService service = null; String newJson=strJson(content,0); service = (IService)BeanService.getBean("shtg"); str=service.doExecute(newJson);//对应Shtg类 newJson=strJson(content,1); service = (IService)BeanService.getBean("jyztUpdate"); str=service.doExecute(newJson); //对应JyztUpdate 类 } public void destroy(){ } } public class Shtg{ public String doExecute(String jsonStr) throws RuntimeException { String sql= "update GadwRyXx set jyzt = :jyzt,kyzt='Addd' where guid = :guid and kyzt = '1'"; baseDao.update(sql, parameters); } } public class JyztUpdate{ public String doExecute(String jsonStr) throws RuntimeException { baseDao.saveObj(gsh); } } 针对doPost和doExecute做了事务配置 此处去除对POST的配置<!--<tx:method name="doPost" propagation="REQUIRED" />--> <tx:method name="doExecute" propagation="REQUIRED" />
  • 打赏
  • 举报
回复
spring的事务管理不到severlet
myepoch 2014-01-27
  • 打赏
  • 举报
回复
真实的代码 public class EntryServlet extends HttpServlet{ public void init(ServletConfig config) throws ServletException { //System.out.println("DB init 初始化"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost( request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ IService service = null; String newJson=strJson(content,0); service = (IService)BeanService.getBean("shtg"); str=service.doExecute(newJson);//对应Shtg类 newJson=strJson(content,1); service = (IService)BeanService.getBean("jyztUpdate"); str=service.doExecute(newJson); //对应JyztUpdate 类 } public void destroy(){ } } public class Shtg{ public String doExecute(String jsonStr) throws RuntimeException { String sql= "update GadwRyXx set jyzt = :jyzt,kyzt='Addd' where guid = :guid and kyzt = '1'"; baseDao.update(sql, parameters); } } public class JyztUpdate{ public String doExecute(String jsonStr) throws RuntimeException { baseDao.saveObj(gsh); } } 针对doPost和doExecute做了事务配置 <tx:method name="doPost" propagation="REQUIRED" /> <tx:method name="doExecute" propagation="REQUIRED" />

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧