JDBC对于事物的支持

并不是所有的数据库都支持事务,即使支持事务的数据库也并非支持所有的事务隔离级别,用户可以通过Connection.getMetaData()方法获取DatabaseMetaData对象,并通过该对象的supportsTransactions()、supportsTransactionIsolationLevel(int level) 、boolean supportsSavepoints()方法查看底层数据库的事务支持情况。

Connection默认情况下是自动提交的,也即每条执行的SQL都对应一个事务,为了能够将多条SQL当成一个事务执行,必须先通过Connection的setAutoCommit(false)阻止Connection自动提交,并可通过Connection的setTransactionIsolation()设置事务的隔离级别,Connection中定义了对应SQL 92标准4个事务隔离级别的常量。通过Connection的commit()提交事务,通过Connection的rollback()回滚事务。

/**
 * 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
 * TRANSACTION_REPEATABLE_READ and further prohibits the
 * situation where one transaction reads all rows that satisfy
 * a WHERE condition, a second transaction inserts a row that
 * satisfies that WHERE condition, and the first transaction
 * rereads for the same condition, retrieving the additional
 * "phantom" row in the second read.
 */
int TRANSACTION_SERIALIZABLE     = 8;

下面是典型的JDBC事务数据操作的代码:

  1. Connection conn ;

  2. try{

  3. conn = DriverManager.getConnection();//①获取数据连接

  4. conn.setAutoCommit(false); //②关闭自动提交的机制

  5. //③设置事务隔离级别

  6. conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);

  7. Statement stmt = conn.createStatement();

  8. int rows = stmt.executeUpdate( "INSERT INTO t_topic ALUES(1,’tom’) " );

  9. rows = stmt.executeUpdate( "UPDATE t_user set topic_nums = topic_nums +1 "+ "WHERE user_id = 1");

  10. conn.commit();//④提交事务

  11. }catch(Exception e){

  12. conn.rollback();//⑤回滚事务

  13. }finally{

  14. }

在JDBC 2.0中,事务最终只能有两个操作:提交和回滚。但是,有些应用可能需要对事务进行更多的控制,而不是简单地提交或回滚。JDBC 3.0(JDK 1.4及以后的版本)引入了一个全新的保存点特性,Savepoint 接口允许用户将事务分割为多个阶段,用户可以指定回滚到事务的特定保存点,而并非像JDBC 2.0一样只回滚到开始事务的点,如下图所示

下面的代码使用了保存点的功能,在发生特定问题时,回滚到指定的保存点,而非回滚整个事务:

  1. Statement stmt = conn.createStatement();

  2. int rows = stmt.executeUpdate( "INSERT INTO t_topic VALUES(1,’tom’)");

  3. Savepoint svpt = conn.setSavepoint("savePoint1");//①设置一个保存点

  4. rows = stmt.executeUpdate( "UPDATE t_user set topic_nums = topic_nums +1 "+ "WHERE user_id = 1");

  5. //②回滚到①处的savePoint1,①之前的SQL操作,在整个事务提交后依然提交,

  6. //但①到②之间的SQL操作被撤销了

  7. conn.rollback(svpt);

  8. conn.commit();//③提交事务

并非所有数据库都支持保存点功能,用户可以通过DatabaseMetaData的supportsSavepoints()方法查看是否支持。

事务类型

数据库事务类型有本地事务和分布式事务:

  • 本地事务:就是普通事务,能保证单台数据库上的操作的ACID,被限定在一台数据库上;
  • 分布式事务:涉及两个或多个数据库源的事务,即跨越多台同类或异类数据库的事务(由每台数据库的本地事务组成的),分布式事务旨在保证这些本地事务的所有操作的ACID,使事务可以跨越多台数据库;

Java事务类型有JDBC事务和JTA事务:

  • JDBC事务:就是数据库事务类型中的本地事务,通过Connection对象的控制来管理事务;
  • JTA事务:JTA指Java事务API(Java Transaction API),是Java EE数据库事务规范, JTA只提供了事务管理接口,由应用程序服务器厂商(如WebSphere Application Server)提供实现,JTA事务比JDBC更强大,支持分布式事务。

Java EE事务类型有本地事务和全局事务:

  • 本地事务:使用JDBC编程实现事务;
  • 全局事务:由应用程序服务器提供,使用JTA事务;

按是否通过编程实现事务有声明式事务和编程式事务;

  • 声明式事务: 通过注解或XML配置文件指定事务信息;
  • 编程式事务:通过编写代码实现事务。

你可能感兴趣的:(JDBC对于事物的支持)