Code Monkey home page Code Monkey logo

spring-'s Introduction

声明式事务

项目结构:

Dao:进行sql语句的执行

Service:进行Dao层的调用并打印结果

resources-applicationContext.xml:启用包扫描,引入外部配置文件,配置数据库连接池,配置JdbcTemplate

test-java-JdbcTemplateTest:测试

声明式事务:

​ 以前通过复杂的编程来编写一个事务,替换为只需要告诉Spring哪个方法是事务方法即可;

​ Spring自动进行事务控制;

编程式事务:

  • 弊端:需要自己拦截那些需要进行事务控制的方法
TransactionFilter{

	try{

		//获取连接

		//设置非自动提交

		chain.doFilter();

		//提交

	}catch(Exception e){

		//回滚

	}finally{

		//关闭连接释放资源

}

}

AOP:环绕通知可以去做;

	//获取连接
	//设置非自动提交
	目标代码执行
    //正常提交
    //异常回滚
    //最终关闭

最终效果:

//我们只需要在需要进行事务控制的方法上添加一行注解
BookService{
    @this is a txmethod(Transactional)
    public void checkout(){
        //xxxx
    }
}

事务管理代码的固定模式作为一种横切关注点,可以通过AOP方法模块化,进而借助Spring AOP框架实现声明式事务管理。

自己要写切面还是很麻烦;在多线程下每个人拿的Connection都不一样,所以需要保证线程同步,还要感知到每个线程的异常,决定哪些回滚哪些不要回滚;

故这个切面已经有了;(事务切面===事务管理器);

DataSourceTransactionManager这个事务管理器就可以在目标方法运行前后进行事务控制(事务切面);

我们目前都使用这个类;即可;spring也提供了其他数据源对应的类入jpa等


快速地位某个方法添加事务:

  1. 配置出这个事务管理器,让他工作;

spring-'s People

Contributors

mhnn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.