このコンテンツは選択した言語では利用できません。

11.7.3. Begin a Transaction


This procedure shows how to begin a new JTA transaction, or how to participate in a distributed transaction using the Java Transaction Service (JTS) protocol. For more information about distributed transactions, refer About Distributed Transactions section.
  1. Get an instance of UserTransaction.

    You can get the instance using JNDI, injection, or an EJB's context, if the EJB uses bean-managed transactions, by means of a @TransactionManagement(TransactionManagementType.BEAN) annotation.
    • JNDI

      new InitialContext().lookup("java:comp/UserTransaction")
      Copy to Clipboard Toggle word wrap
    • Injection

      @Resource UserTransaction userTransaction;
      Copy to Clipboard Toggle word wrap
    • Context

      • In a stateless/stateful bean:
        @Resource SessionContext ctx;
        ctx.getUserTransaction();
        Copy to Clipboard Toggle word wrap
      • In a message-driven bean:
        @Resource MessageDrivenContext ctx;
        ctx.getUserTransaction()
        Copy to Clipboard Toggle word wrap
  2. Call UserTransaction.begin() after you connect to your datasource.

    ...
    try {
        System.out.println("\nCreating connection to database: "+url);
        stmt = conn.createStatement();  // non-tx statement
        try {
            System.out.println("Starting top-level transaction.");
            userTransaction.begin();
            stmtx = conn.createStatement(); // will be a tx-statement
            ...
        }
    }
    
    Copy to Clipboard Toggle word wrap
Participate in an existing transaction using the JTS API.

One of the benefits of EJBs is that the container manages all of the transactions. If you have set up the ORB and activated JTS transactions, the container will manage distributed transactions for you.

Result:

The transaction begins. All uses of your datasource until you commit or roll back the transaction are transactional.

Note

トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2025 Red Hat