Version 2

    &12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531; (Application Transaction)

     

    &12399;&12376;&12417;&12395; (Introduction)

    The standard JTA transaction is thread-based. The transaction has to commit or rollback at the end of a thread. At the end of the thread, the transaction is committed; the data changes are flushed to the database; and the persistence context for the EntityManager is destroyed. That creates some problems for web applications, where each web request is its own thread but multiple requests can be logically tied into a transactional session. For instance, let's consider a shopping cart application. The user needs to go through multiple web pages: to select the products, to enter the credit card information, to enter shipping address, to review the final cost, and then finally to finish the entire transaction. Each page submission is processed by a separate thread on the server and the order should only be committed to the database after the last page. That is difficult to implement with the JTA transaction managers.

     

    &27161;&28310;&30340;&12394;JTA&12398;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531;&12399;&12473;&12524;&12483;&12489;&12505;&12540;&12473;&12391;&12377;&12290;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531;&12399;&12473;&12524;&12483;&12489;&12398;&26368;&24460;&12395;&12467;&12511;&12483;&12488;&12363;&12525;&12540;&12523;&12496;&12483;&12463;&12375;&12394;&12369;&12428;&12400;&12394;&12426;&12414;&12379;&12435;&12290;&12473;&12524;&12483;&12489;&12398;&26368;&24460;&12395;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531;&12399;&12467;&12511;&12483;&12488;&12373;&12428;&12289;&12487;&12540;&12479;&12398;&22793;&26356;&12399;&12487;&12540;&12479;&12505;&12540;&12473;&12408;&12501;&12521;&12483;&12471;&12517;&12373;&12428;&12289;EntityManager&12398;&27704;&32154;&24615;&12467;&12531;&12486;&12461;&12473;&12488;&12399;&30772;&22730;&12373;&12428;&12414;&12377;&12290;Web&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12395;&12392;&12387;&12390;&12399;&12289;&12371;&12428;&12399;&12356;&12367;&12388;&12363;&12398;&21839;&38988;&12395;&12394;&12426;&12414;&12377;&12290;Web&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12391;&12399;&21508;Web&12522;&12463;&12456;&12473;&12488;&12399;&29420;&33258;&12398;&12473;&12524;&12483;&12489;&12391;&21205;&20316;&12375;&12414;&12377;&12364;&12289;&35079;&25968;&12398;&12522;&12463;&12456;&12473;&12488;&12364;&35542;&29702;&30340;&12395;&12402;&12392;&12388;&12398;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531;&12475;&12483;&12471;&12519;&12531;&12395;&32080;&12403;&20184;&12369;&12425;&12428;&12427;&12371;&12392;&12364;&12354;&12427;&12363;&12425;&12391;&12377;&12290;&20363;&12392;&12375;&12390;&12471;&12519;&12483;&12500;&12531;&12464;&12459;&12540;&12488;&12398;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12434;&32771;&12360;&12414;&12375;&12423;&12358;&12290;&12518;&12540;&12470;&12399;&21830;&21697;&12434;&36984;&12403;&12289;&12463;&12524;&12472;&12483;&12488;&12459;&12540;&12489;&24773;&22577;&12434;&20837;&21147;&12375;&12289;&30330;&36865;&20808;&12434;&20837;&21147;&12375;&12289;&26368;&32066;&30340;&12394;&20385;&26684;&12434;&30906;&35469;&12375;&12289;&26368;&24460;&12395;&20840;&20307;&12398;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531;&12434;&32066;&20102;&12377;&12427;&12383;&12417;&12395;&12289;&35079;&25968;&12398;Web&12506;&12540;&12472;&12434;&31227;&21205;&12377;&12427;&24517;&35201;&12364;&12354;&12426;&12414;&12377;&12290;&21508;&12506;&12540;&12472;&12391;&12398;&12469;&12502;&12511;&12483;&12488;&12399;&12469;&12540;&12496;&19978;&12398;&21029;&12293;&12398;&12473;&12524;&12483;&12489;&12391;&20966;&29702;&12373;&12428;&12414;&12377;&12364;&12289;&27880;&25991;&12364;&12487;&12540;&12479;&12505;&12540;&12473;&12408;&12467;&12511;&12483;&12488;&12373;&12428;&12427;&12398;&12399;&26368;&24460;&12398;&12506;&12540;&12472;&12398;&24460;&12398;&12415;&12391;&12354;&12427;&12409;&12365;&12391;&12377;&12290;&12371;&12428;&12434;JTA&12398;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531;&12510;&12493;&12540;&12472;&12515;&12391;&23455;&35013;&12377;&12427;&12398;&12399;&22256;&38627;&12391;&12377;&12290;

     

    In EJB 3.0, there is an option to make the EntityManager "long lived" to support multi-thread transactions (i.e., application transactions) in stateful session beans. The long-lived EntityManager does not commit until the host stateful session bean instance is destroyed (i.e., the persistence context is destroyed). In this trail, we will cover how to use application transactions.

     

    EJB 3.0&12391;&12399;&12289;&12473;&12486;&12540;&12488;&12501;&12523;&12475;&12483;&12471;&12519;&12531;Bean&12391;&12398;&12510;&12523;&12481;&12473;&12524;&12483;&12489;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531; (&12377;&12394;&12431;&12385;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531;)&12434;&12469;&12509;&12540;&12488;&12377;&12427;&12383;&12417;EntityManager&12434;&12300;&38263;&26399;&29983;&23384;&12373;&12379;&12427;&12301;&12458;&12503;&12471;&12519;&12531;&12364;&12354;&12426;&12414;&12377;&12290;&38263;&26399;&29983;&23384;EntityManager&12399;&12507;&12473;&12488;&12392;&12394;&12427;&12473;&12486;&12540;&12488;&12501;&12523;&12475;&12483;&12471;&12519;&12531;Bean&12398;&12452;&12531;&12473;&12479;&12531;&12473;&12364;&30772;&22730;&12373;&12428;&12427; (&12377;&12394;&12431;&12385;&27704;&32154;&24615;&12467;&12531;&12486;&12461;&12473;&12488;&12364;&30772;&22730;&12373;&12428;&12427;)&12414;&12391;&12467;&12511;&12483;&12488;&12375;&12414;&12379;&12435;&12290;&12371;&12398;&23567;&36947;&12391;&12399;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531;&12398;&20351;&12356;&26041;&12434;&21462;&12426;&19978;&12370;&12414;&12377;&12290;

     

    &12469;&12531;&12503;&12523;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531; (The sample application)

    The sample application in this trail is a simple variation of the "currency exchange rate update" example for the investment calculator we have seen earlier in the previous trail. After you submit a new currency exchange rate, the application updates all the calculation records in the entity bean objects but it does not flush the changes to the database yet. The application then displays another page asking you whether to update the timestamp of those records. If you choose "yes", the timestamp attribute of the bean objects are also updated and all the changes are flushed to the database in a single batch.

     

    &12371;&12398;&23567;&36947;&12398;&12469;&12531;&12503;&12523;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12399;&12289;&21069;&12398;&23567;&36947;&12391;&35211;&12383;&25237;&36039;&35336;&31639;&12398;&12383;&12417;&12398;&12300;&28858;&26367;&12524;&12540;&12488;&26356;&26032;&12301;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12398;&21336;&32020;&12394;&12496;&12522;&12456;&12540;&12471;&12519;&12531;&12391;&12377;&12290;&26032;&12375;&12356;&28858;&26367;&12524;&12540;&12488;&12434;&12469;&12502;&12511;&12483;&12488;&12377;&12427;&12392;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12399;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12458;&12502;&12472;&12455;&12463;&12488;&12398;&12377;&12409;&12390;&12398;&35336;&31639;&12524;&12467;&12540;&12489;&12434;&26356;&26032;&12375;&12414;&12377;&12364;&12289;&12414;&12384;&22793;&26356;&12434;&12487;&12540;&12479;&12505;&12540;&12473;&12408;&12501;&12521;&12483;&12471;&12517;&12375;&12414;&12379;&12435;&12290;&12381;&12375;&12390;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12399;&27425;&12398;&12506;&12540;&12472;&12434;&34920;&31034;&12375;&12289;&12371;&12428;&12425;&12398;&12524;&12467;&12540;&12489;&12398;&12479;&12452;&12512;&12473;&12479;&12531;&12503;&12434;&26356;&26032;&12377;&12427;&12363;&12393;&12358;&12363;&12434;&23563;&12397;&12414;&12377;&12290;&12300;&12399;&12356;&12301;&12434;&36984;&25246;&12377;&12428;&12400;&12289;Bean&12458;&12502;&12472;&12455;&12463;&12488;&12398;&12479;&12452;&12512;&12473;&12479;&12531;&12503;&23646;&24615;&12418;&26356;&26032;&12373;&12428;&12289;&12377;&12409;&12390;&12398;&22793;&26356;&12364;&12402;&12392;&12388;&12398;&12496;&12483;&12481;&20966;&29702;&12391;&12487;&12540;&12479;&12505;&12540;&12473;&12395;&12501;&12521;&12483;&12471;&12517;&12373;&12428;&12414;&12377;&12290;

     

    &25313;&24373;EntityManager (The extended EntityManager)

    To make the application transaction work, you simply need to specify the type attribute of the the @PersistenceContext injection annotation as PersistenceContextType.EXTENDED. It tells the container that the EntityManager object in this bean maintains its persistence context (i.e., the beans it manages) at the end of a thread or method call.

     

    &12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531;&12434;&20351;&12358;&12395;&12399;&12289;@PersistenceContext&12452;&12531;&12472;&12455;&12463;&12471;&12519;&12531;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12398;type&23646;&24615;&12395;PersistenceContextType.EXTENDED&12434;&25351;&23450;&12377;&12427;&12384;&12369;&12391;&12377;&12290;&12381;&12428;&12399;&12467;&12531;&12486;&12490;&12395;&12289;&12371;&12398;Bean&12398;EntityManager&12458;&12502;&12472;&12455;&12463;&12488;&12364;&12473;&12524;&12483;&12489;&12414;&12383;&12399;&12513;&12477;&12483;&12489;&21628;&12403;&20986;&12375;&12398;&26368;&24460;&12391;&12381;&12398;&27704;&32154;&24615;&12467;&12531;&12486;&12461;&12473;&12488; (&12377;&12394;&12431;&12385;EntityManager&12364;&31649;&29702;&12377;&12427;Bean)&12434;&32173;&25345;&12377;&12427;&12424;&12358;&12289;&20253;&12360;&12414;&12377;&12290;

     

     @Stateful
     public class ApptransCalculator implements Calculator, Serializable {
     
       @PersistenceContext(
           type=PersistenceContextType.EXTENDED
       )
       protected EntityManager em;
       
       // ... ...
      
     }
    

     

    &12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531;&12434;&12467;&12511;&12483;&12488;&12377;&12427; (Commit the transaction)

    As we described, the EntityManager only updates the database when the stateful session is destroyed. Recall that we can destroy a stateful session bean by calling the @Remove tagged method. We also call the EntityManager.flush() method in the @Remove method to make sure that database is synchronized.

     

    &35500;&26126;&12375;&12383;&12424;&12358;&12395;&12289;EntityManager&12399;&12473;&12486;&12540;&12488;&12501;&12523;&12475;&12483;&12471;&12519;&12531;Bean&12364;&30772;&22730;&12373;&12428;&12383;&12392;&12365;&12398;&12415;&12487;&12540;&12479;&12505;&12540;&12473;&12434;&26356;&26032;&12375;&12414;&12377;&12290;@Remove&12434;&12479;&12464;&20184;&12369;&12375;&12383;&12513;&12477;&12483;&12489;&12434;&21628;&12409;&12400;&12473;&12486;&12540;&12488;&12501;&12523;&12475;&12483;&12471;&12519;&12531;Bean&12434;&30772;&22730;&12391;&12365;&12427;&12371;&12392;&12434;&24605;&12356;&20986;&12375;&12390;&12367;&12384;&12373;&12356;&12290;&12487;&12540;&12479;&12505;&12540;&12473;&12364;&21516;&26399;&21270;&12373;&12428;&12427;&12371;&12392;&12434;&30906;&23455;&12395;&12377;&12427;&12383;&12417;&12289;@Remove&12513;&12477;&12483;&12489;&20869;&12391;EntityManager.flush()&12513;&12477;&12483;&12489;&12434;&21628;&12406;&12424;&12358;&12395;&12418;&12375;&12390;&12356;&12414;&12377;&12290;

     

     @Stateful
     public class ApptransCalculator implements Calculator, Serializable {
     
       // ... ...
       
       @TransactionAttribute(TransactionAttributeType.REQUIRED)
       public void updateExchangeRate (double newrate) {
         // ... ...
       }
     
       @TransactionAttribute(TransactionAttributeType.REQUIRED)
       public void updateTimestamp () {
         // ... ...
       }
       
       @Remove
       public void checkout() throws Exception {
         em.flush ()
       }
     
     }
    

     

    &23436;&20840;&12394;&12477;&12540;&12473;&12467;&12540;&12489; (Complete source code)

    Server

     

    &12469;&12540;&12496;

     

    Calculator.java: The local interface of the CalculatorBean session bean

     

    CalculatorBean&12475;&12483;&12471;&12519;&12531;Bean&12398;&12525;&12540;&12459;&12523;&12452;&12531;&12479;&12540;&12501;&12455;&12540;&12473;

     

    ApptransCalculator.java: The stateful session bean with an extended application transaction

     

    &25313;&24373;&12375;&12383;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531;&23550;&24540;&12398;&12473;&12486;&12540;&12488;&12501;&12523;&12475;&12483;&12471;&12519;&12531;Bean

     

    Client

     

    &12463;&12521;&12452;&12450;&12531;&12488;

     

    calculator.jsp: The client JSP that performs the first stage of the update transaction

     

    &26356;&26032;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531;&12398;&31532;&19968;&27573;&38542;&12434;&23455;&34892;&12377;&12427;&12463;&12521;&12452;&12450;&12531;&12488;JSP

     

    check.jsp: The client JSP that performs the second stage of the update transaction

     

    &26356;&26032;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531;&12398;&31532;&20108;&27573;&38542;&12434;&23455;&34892;&12377;&12427;&12463;&12521;&12452;&12450;&12531;&12488;JSP

     

    &12414;&12392;&12417; (Summary)

    In this trail, you learned how to use long lived EntityManager object for multi-page application transactions, such as those in a shopping cart application. In the next trail, we will discuss another type of container service for managed objects and methods: security services.

     

    &12371;&12398;&23567;&36947;&12391;&12399;&12289;&12471;&12519;&12483;&12500;&12531;&12464;&12459;&12540;&12488;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12398;&12424;&12358;&12394;&35079;&25968;&12506;&12540;&12472;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12398;&12488;&12521;&12531;&12470;&12463;&12471;&12519;&12531;&12398;&12383;&12417;&12395;&12289;&38263;&26399;&29983;&23384;EntityManager&12458;&12502;&12472;&12455;&12463;&12488;&12434;&20351;&12358;&26041;&27861;&12434;&23398;&12403;&12414;&12375;&12383;&12290;&27425;&12398;&23567;&36947;&12391;&12399;&12289;&31649;&29702;&23550;&35937;&12458;&12502;&12472;&12455;&12463;&12488;&12392;&12513;&12477;&12483;&12489;&12398;&12383;&12417;&12398;&21029;&12479;&12452;&12503;&12398;&12467;&12531;&12486;&12490;&12469;&12540;&12499;&12473;&12391;&12354;&12427;&12475;&12461;&12517;&12522;&12486;&12451;&12469;&12540;&12499;&12473;&12395;&12388;&12356;&12390;&35542;&12376;&12414;&12377;&12290;

     

    © 2005 JBoss, Inc. All Rights Reserved