spring声明式事务学习笔记

步骤

1、导入相关依赖: spring-jdbc等
2、配置数据源JdbcTemplate
3、开启基于注解的事务管理 @EnableTransactionManagement
4、配置事务管理器来控制事务【public PlatformTransactionManager transactionManager ()】
5、给业务service方法上标注@Transactional,表示当前方法是一个事务方法

class UserService {
    @Autowired
    UserDao userDao;
    
     @Transactional
    public inser(){
          userDao.inser();
          userDao.update();
    }
}

ComboPooledDataSource 需要的jar

<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
@Bean
public DataSource dataSource(){
	ComboPooledDataSource dataSource = new ComboPooledDataSource();
	dataSource.setUser(root); 
	dataSource.setPassword(123456); 
	dataSource.setJdbcUrl(jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8"); 
	dataSource.setDriverClass(com.mysql.jdbc.Driver); 
	dataSource.setInitialPoolSize(); 
	dataSource.setAcquireIncrement(); 
	dataSource.setMinPoolSize();
	dataSource.setMaxPoolSize();
	dataSource.setMaxStatements(); 
	dataSource.setMaxIdleTime(); 
	dataSource.setIdleConnectionTestPeriod(); 
	dataSource.setAcquireRetryAttempts();
	return  dataSource

}

@Bean
public PlatformTransactionManager  transactionManager (){

    return new PlatformTransactionManage(dataSource())  
}

c3p0数据源ComboPooledDataSource的详细配置

1、基础配置
driverclass:数据库驱动类,比如MySQL为
com.mysql.jdbc.Driver
jdbcUrl: 数据库连接,比如
jdbc:mysql://127.0.0.1:3306/girl?useUnicode=true&characterEncoding=utf-8&useSSL=false
user:登录数据库的账号,比如
root
password:登录数据库的密码,比如
123456
2、私有配置
c3p0.acquireIncrement=20
当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3

c3p0.minPoolSize=20
连接池中保留的最小连接数,默认为:3

c3p0.maxPoolSize=100
接池中保留的最大连接数。默认值: 15

c3p0.initialPoolSize=20
初始化时获取连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3
c3p0.maxIdleTime=60
最大空闲时间,多少秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0

c3p0.maxStatements=0
c3p0全局的PreparedStatements缓存的大小。如果maxStatements与maxStatementsPerConnection均为0,则缓存不生效,只要有一个不

为0,则语句的缓存就能生效。如果默认值: 0

c3p0.numHelperThreads=10
c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能通过多线程实现多个操作同时被执行。Default: 3

c3p0.acquireRetryAttempts=5
定义在从数据库获取新连接失败后重复尝试的次数。默认值: 30 ;小于等于0表示无限次

c3p0.acquireRetryDelay=300
重新尝试的时间间隔,默认为:1000毫秒

c3p0.checkoutTimeout=3000
获取一个connection超时时间,单位毫秒

c3p0.idleConnectionTestPeriod=60
每隔多少秒检查所有连接池中的空闲连接。Default: 0

c3p0.automaticTestTable=sys_connectiontest
c3p0将建一张名为改配置项的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么属性preferredTestQuery将#被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试使用。默认值: null。由于运营平台的数据库用户没有创建表的权限,故需要发sql创建表。

c3p0.testConnectionOnCheckin=true
如果设为true那么在取得连接的同时将校验连接的有效性。Default: false

c3p0.unreturnedConnectionTimeout=15
一个checkout连接的超时设置,一旦一个checkout连接超时,他将物理的关闭,而不是返回池中,主要是防止连接被长期使用不释放,这个设置也是比较危险的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值