Home  >  Article  >  Java  >  What are the types of java transactions and how to use them

What are the types of java transactions and how to use them

王林
王林forward
2023-05-08 21:55:211672browse

1.JDBC transaction

Use Connection object to control, JDBC’s Connection interface provides two transaction modes: automatic submission and manual submission.

 public void setAutoCommit(boolean)
 public boolean getAutoCommit()
 public void commit()
 public void rollback()

JDBC provides the most basic support for using Java to perform database transaction operations. Multiple SQL statements can be put into the same transaction to ensure their ACID properties.

When it comes to multi-database operations or distributed scenarios, JDBC transactions are powerless.

2.JTA (Java Transaction API) transaction

JTA is a high-level, implementation-independent, and protocol-independent API , applications and application servers can use JTA to access transactions. JTA allows applications to perform distributed transactions - accessing and updating data on two or more network computer resources, which can be distributed across multiple databases. The JDBC driver's JTA support greatly enhances data access capabilities.

public void JtaTransfer() { 
        javax.transaction.UserTransaction tx = null;
        java.sql.Connection conn = null;
         try{ 
             tx = (javax.transaction.UserTransaction) context.lookup("java:comp/UserTransaction");  //取得JTA事务,本例中是由Jboss容器管理
             javax.sql.DataSource ds = (javax.sql.DataSource) context.lookup("java:/XAOracleDS");  //取得数据库连接池,必须有支持XA的数据库、驱动程序  
             tx.begin();
            conn = ds.getConnection();
             // 将自动提交设置为 false,
             //若设置为 true 则数据库将会把每一次数据更新认定为一个事务并自动提交
             conn.setAutoCommit(false);
             stmt = conn.createStatement(); 
             // 将 A 账户中的金额减少 500 
             stmt.execute("\
             update t_account set amount = amount - 500 where account_id = 'A'");
             // 将 B 账户中的金额增加 500 
             stmt.execute("\
             update t_account set amount = amount + 500 where account_id = 'B'");
             // 提交事务
             tx.commit();
             // 事务提交:转账的两步操作同时成功
         } catch(SQLException sqle){            
             try{ 
                 // 发生异常,回滚在本事务中的操做
              tx.rollback();
                 // 事务回滚:转账的两步操作完全撤销
                 stmt.close(); 
                 conn.close(); 
             }catch(Exception ignore){ 
             } 
             sqle.printStackTrace(); 
         } 
     }

3. Container transaction

Container transaction is mainly provided by the J2EE application server. Container transactions are mostly completed based on JTA. This is a Based on JNDI, quite complex API implementation.

The above is the detailed content of What are the types of java transactions and how to use them. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete