MySQL事务

事务基础知识:[url]http://my.oschina.net/jeffli1993/blog/684762[/url]
数据库事务隔离级别-- 脏读、幻读、不可重复读(清晰解释) :[url]http://blog.csdn.net/jiesa/article/details/51317164[/url]
1.测试类
public class TestMySQL {
public static void main(String[] args) {
String path = Thread.currentThread().getContextClassLoader ().getResource("").toString();
System.out.println("==========currentThread:"+path);
TestMySQL tm = new TestMySQL();
System.out.println("==========File:"+tm.getClass().getResource("").getFile());
System.out.println("==========Path:"+tm.getClass().getResource("").getPath());
String jdbcPath = tm.getClass().getResource("").getPath();
try {
File pFile = new File(jdbcPath+"jdbc.properties");
FileInputStream pInStream=null;
try {
pInStream = new FileInputStream(pFile );
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Properties pInfo = new Properties();
try {
pInfo.load(pInStream );
} catch (IOException e) {
e.printStackTrace();
}
String drive = pInfo.getProperty("driver");
String url = pInfo.getProperty("url");
String user = pInfo.getProperty("user");
String pwd = pInfo.getProperty("pwd");
Class.forName(drive);
Connection conn = DriverManager.getConnection(url, user, pwd);
DatabaseMetaData metaData = conn.getMetaData();
// 是否支持的事务
boolean isSupport = metaData.supportsTransactions();
System.out.println("======isSupportsTransactions:"+isSupport);
// 是否支持的Connection.TRANSACTION_SERIALIZABLE等级事务
boolean isSupportLevel = metaData
.supportsTransactionIsolationLevel(Connection.TRANSACTION_SERIALIZABLE);
System.out.println("======isSupportLevel:"+isSupportLevel);
// 获取默认事务
int defaultIsolation = metaData.getDefaultTransactionIsolation();
System.out.println("======DefaultTransactionIsolation:"+defaultIsolation);
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();

}

}
}

2.控制台输出如下:
==========currentThread:file:/F:/Myeclipse/test/WebRoot/WEB-INF/classes/
==========File:/F:/Myeclipse/test/WebRoot/WEB-INF/classes/com/test/controller/test/
==========Path:/F:/Myeclipse/test/WebRoot/WEB-INF/classes/com/test/controller/test/
======isSupportsTransactions:true
======isSupportLevel:true
======DefaultTransactionIsolation:2
3.从上可以看出Mysql默认支持的事物为:
/**
* A constant indicating that
* dirty reads are prevented; non-repeatable reads and phantom
* reads can occur. This level only prohibits a transaction
* from reading a row with uncommitted changes in it.
*/
int TRANSACTION_READ_COMMITTED = 2;
可以避免脏数据的发生,但不重读,幻读可能发生。
4.从Connection的源码中,我们可以看到事务等级的定义如下:
 /** 不支持事务
* A constant indicating that transactions are not supported.
*/
int TRANSACTION_NONE = 0;

/**支持事务,脏数据,不重读,幻读可能发生。
* A constant indicating that
* dirty reads, non-repeatable reads and phantom reads can occur.
* This level allows a row changed by one transaction to be read
* by another transaction before any changes in that row have been
* committed (a "dirty read"). If any of the changes are rolled back,
* the second transaction will have retrieved an invalid row.
*/
int TRANSACTION_READ_UNCOMMITTED = 1;

/**支持事务,不重读,幻读可能发生。
* A constant indicating that
* dirty reads are prevented; non-repeatable reads and phantom
* reads can occur. This level only prohibits a transaction
* from reading a row with uncommitted changes in it.
*/
int TRANSACTION_READ_COMMITTED = 2;

/**支持事务,幻读可能发生。
* A constant indicating that
* dirty reads and non-repeatable reads are prevented; phantom
* reads can occur. This level prohibits a transaction from
* reading a row with uncommitted changes in it, and it also
* prohibits the situation where one transaction reads a row,
* a second transaction alters the row, and the first transaction
* rereads the row, getting different values the second time
* (a "non-repeatable read").
*/
int TRANSACTION_REPEATABLE_READ = 4;

/**支持事务,脏数据,不重读,幻读不可能发生。
* A constant indicating that
* dirty reads, non-repeatable reads and phantom reads are prevented.
* This level includes the prohibitions in
* <code>TRANSACTION_REPEATABLE_READ</code> and further prohibits the
* situation where one transaction reads all rows that satisfy
* a <code>WHERE</code> condition, a second transaction inserts a row that
* satisfies that <code>WHERE</code> condition, and the first transaction
* rereads for the same condition, retrieving the additional
* "phantom" row in the second read.
*/
int TRANSACTION_SERIALIZABLE = 8;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值