what is JTA

최대 1 분 소요

JTA

JTA == Java Transaction API Java 진영에서 transaction 을 관리하기위한 API

The true power of JTA lies in its ability to manage multiple resources (i.e. databases, messaging services) in a single transaction.

transaction(commit or rollback)을 제어하기위해 추상적으로 제공 해당 추상화가 없는경우, 각 리소스별로 각 API에 대해서 처리를 해야함

JDBC 예외처리 예제

  public void updateCoffeeSales(HashMap<String, Integer> salesForWeek) throws SQLException {
    String updateString =
      "update COFFEES set SALES = ? where COF_NAME = ?";
    String updateStatement =
      "update COFFEES set TOTAL = TOTAL + ? where COF_NAME = ?";

    try (PreparedStatement updateSales = con.prepareStatement(updateString);
         PreparedStatement updateTotal = con.prepareStatement(updateStatement))
    
    {
      con.setAutoCommit(false);
      for (Map.Entry<String, Integer> e : salesForWeek.entrySet()) {
        updateSales.setInt(1, e.getValue().intValue());
        updateSales.setString(2, e.getKey());
        updateSales.executeUpdate();

        updateTotal.setInt(1, e.getValue().intValue());
        updateTotal.setString(2, e.getKey());
        updateTotal.executeUpdate();
        con.commit();
      }
    } catch (SQLException e) {
      JDBCTutorialUtilities.printSQLException(e);
      if (con != null) {
        try {
          System.err.print("Transaction is being rolled back");
          con.rollback();
        } catch (SQLException excep) {
          JDBCTutorialUtilities.printSQLException(excep);
        }
      }
    }

JTA를 이용하면 많은 다양한 리소스들을 일관성있게 관리 OK

카테고리:

업데이트: