概述
Jfinal事务原理
通过对方法增加全局拦截器,执行对应方法时,被事务拦截器拦截,当执行到拦截器链的后半段而没有异常抛出时,执行事务的commit()。有异常抛出则执行事务的回滚方法rollback()。1.第一步给指定的方法添加拦截器的方式①在启动配置文件中,通过正则匹配方法,能够匹配到的方法,将添加事务拦截器
②通过注解Before(Tx.class)为相应的方法添加事务拦截器,此方法被请求时将添加事务拦截器
③如果有自定义注解@interface Service,且@interface Service有关联事务连接器也可以通过在service中开启事务(说明:我们项目中施用Enhance对开启事务的service注解,进行添加事务)
2.第二步就是拦截器中对事务的实现,采用JDBC事务实现
①创建连接Connection conn = config.getThreadLocalConnection();
if (conn != null) {// Nested transaction support
try {
if (conn.getTransactionIsolation() < getTransactionLevel(config))
conn.setTransactionIsolation(getTransactionLevel(config));
inv.invoke();
return ;
} catch (SQLException e) {
throw new ActiveRecordException(e);
}
}
Boolean autoCommit = null;
try {
conn = config.getConnection();
②设置自动提交为falseautoCommit = conn.getAutoCommit();
config.setThreadLocalConnection(conn);
conn.setTransactionIsolation(getTransactionLevel(config));// conn.setTransactionIsolation(transactionLevel);
conn.setAutoCommit(false);
inv.invoke();
③执行完没有异常抛出则提交inv.invoke();
conn.commit();
④有异常则捕获异常,回滚if (result)
conn.commit();
else
conn.rollback();
return result;
} catch (NestedTransactionHelpException e) {
if (conn != null) try {conn.rollback();} catch (Exception e1) {LogKit.error(e1.getMessage(), e1);}
LogKit.logNothing(e);
return false;
} catch (Throwable t) {
if (conn != null) try {conn.rollback();} catch (Exception e1) {LogKit.error(e1.getMessage(), e1);}
throw t instanceof RuntimeException ? (RuntimeException)t : new ActiveRecordException(t);
} finally {
最后
以上就是长情豌豆为你收集整理的Jfinal事务的全部内容,希望文章能够帮你解决Jfinal事务所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复