关于Jfinal事务控制的失效的问题

首先说明我的项目能起来,通过路由也能访问到这个test里面去,但是写的两个线程在方法头上加声明式事务不起作用,用debug方式启动项目发现根本就不进Tx.class里面对应的方法,言外之意就是这里写的声明形同虚设,同事们说他们是手动写的,也就是编程式事务,这有违背Jfinal的极简原则,所以能帮忙看看解释下为什么这样写事务不起作用嘛?难道说config里面要配置什么嘛!

public void test() throws Exception{
    Thread1 thread1 = new Thread1();
    FutureTask<HfPartentVirtualAccount> future1 = new FutureTask<HfPartentVirtualAccount>(thread1);
    new Thread(future1).start();
    Thread2 thread2 = new Thread2();
    FutureTask<HfPartentVirtualAccount> future2 = new FutureTask<HfPartentVirtualAccount>(thread2);
    new Thread(future2).start();
    Map ret = new HashMap();
    ret.put("thread1",future1.get().getStr("a_partner_name"));
    ret.put("thread2",future2.get().getStr("a_partner_name"));
    renderJson(ret);
}
class Thread1 implements Callable<HfPartentVirtualAccount> {
    @Before(Tx.class)
    public HfPartentVirtualAccount call() throws Exception {
        System.out.println("=========线程1开始============");
        HfPartentVirtualAccount taskSdh = HfPartentVirtualAccount.dao.findFirst("select * from hf_partner_virtual_account where a_id = 1  for update");
        Thread.sleep(1000*5);
        System.out.println("=========线程1结束============");
        return taskSdh;
    }
}
class Thread2 implements Callable<HfPartentVirtualAccount>{
    @Before(Tx.class)
    public HfPartentVirtualAccount call() throws Exception {
        System.out.println("=========线程2开始============");
        HfPartentVirtualAccount taskSdh = HfPartentVirtualAccount.dao.findFirst("select * from hf_partner_virtual_account where a_id = 1  for update");
        System.out.println("=========线程2结束============");
        return taskSdh;
    }
}

上面这一段运行的结果是

=========线程1开始============
=========线程2开始============
=========线程2结束============
=========线程1结束============

如下图的Tx形同虚设,debug都没进去

image.png

评论区

yangkai

2017-11-22 11:49

已经解决了,看了jfinal的开发pdf找到了这一串内容
// 定义需要使用AOP的业务层类
public class OrderService {
// 配置事务拦截器
@Before(Tx.class)
public void payment(int orderId, int userId) {
// service code here
}
}
// 定义控制器,控制器提供了enhance系列方法可对目标进行AOP增强
public class OrderController extends Controller {
public void payment() {
// 使用 enhance方法对业务层进行增强,使其具有AOP能力
OrderService service = enhance(OrderService.class);
// 调用payment方法时将会触发拦截器
service.payment(getParaToInt("orderId"), getParaToInt("userId"));
}
}

yangkai

2017-11-22 11:51

修改的测试代码
public class HfPartentVirtualAccountService {

public static boolean flat=true;
@Before(Tx.class)
public HfPartentVirtualAccount selectTest() throws Exception{
HfPartentVirtualAccount taskSdh = HfPartentVirtualAccount.dao.findFirst("select * from hf_partner_virtual_account where a_id = 1 for update");
if(flat){
flat = false;
Thread.sleep(1000*5);
}
return taskSdh;
}
}
public void test() throws Exception{
HfPartentVirtualAccountService virtualAccountService = enhance(HfPartentVirtualAccountService.class);
HfPartentVirtualAccountService.flat = true;
Thread1 thread1 = new Thread1(virtualAccountService);
FutureTask future1 = new FutureTask(thread1);
new Thread(future1).start();
Thread2 thread2 = new Thread2(virtualAccountService);
FutureTask future2 = new FutureTask(thread2);
new Thread(future2).start();
Map ret = new HashMap();
ret.put("thread1",future1.get().getStr("a_partner_name"));
ret.put("thread2",future2.get().getStr("a_partner_name"));
renderJson(ret);
}
class Thread1 implements Callable {
HfPartentVirtualAccountService virtualAccountService;
public Thread1(HfPartentVirtualAccountService virtualAccountService){
this.virtualAccountService = virtualAccountService;
}
public HfPartentVirtualAccount call() throws Exception{
System.out.println("=========线程1开始============");
HfPartentVirtualAccount taskSdh = virtualAccountService.selectTest();
System.out.println("=========线程1结束============");
return taskSdh;
}
}

class Thread2 implements Callable{
HfPartentVirtualAccountService virtualAccountService;
public Thread2(HfPartentVirtualAccountService virtualAccountService){
this.virtualAccountService = virtualAccountService;
}
public HfPartentVirtualAccount call() throws Exception{
System.out.println("=========线程2开始============");
HfPartentVirtualAccount taskSdh = virtualAccountService.selectTest();
System.out.println("=========线程2结束============");
return taskSdh;
}
}
出现了传说中的事务等待,不然我这个for update没用啊,哈哈,结贴!

JFinal

2017-11-24 17:55

感谢回来分享,最近忙着发布 jfinal 3.3 , 很多问题都顾不上 ^_^

热门反馈

扫码入社