数据库的事务隔离级别本质是事务并发的处理方式和逻辑问题。
脏读:一个事务读取了另外一个并行事务未提交的数据
不可重复读取:一个事务再次读取之前的数据时得到的数据不一致,被另外一个事务修改,针对更新(update)和删除(delete)操作虚读(幻读):一个事务重新执行一个查询,返回的记录包含了其他事务提交的新记录,针对插入操作(INSERT)
设定事务的隔离级别:con.setTransactionIsolation(Connection.isolationLevel);
四种隔离级别:
con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);//最低级别:只保证不会读到非法数据,上述3个问题有可能发生
con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); //默认级别:可以防止脏读,Oracle 的默认级别
con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);//可以防止脏读和不可重复读取,Mysql 的默认级别
con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); //最高级别:防止上述3种情况,事务串行执行,慎用con.setTransactionIsolation(Connection.TRANSACTION_NONE); //没有事务
下部分代码为实际过程中遇到的bug,四台机群,连接数就爆了。
java.sql.SQLException: javax.resource.ResourceException: IJ000453: Unable to get managed connection for java:jboss/jdbc/csiqcswds
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:240) ~[spring-jdbc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371) ~[spring-tx-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:336) ~[spring-tx-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105) ~[spring-tx-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[org.springframework.aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622) ~[org.springframework.aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]