首先说明我的项目能起来,通过路由也能访问到这个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都没进去
// 定义需要使用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"));
}
}