View Javadoc

1   package org.csc.phynixx.xa;
2   
3   /*
4    * #%L
5    * phynixx-xa
6    * %%
7    * Copyright (C) 2014 csc
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  
24  import org.csc.phynixx.connection.IPhynixxConnection;
25  import org.csc.phynixx.connection.IPhynixxManagedConnection;
26  import org.csc.phynixx.connection.IPhynixxManagedConnectionListener;
27  import org.csc.phynixx.connection.PhynixxManagedConnectionListenerAdapter;
28  
29  import javax.transaction.xa.Xid;
30  
31  
32  /**
33   * This TransactionProxy shows, that the XAresource is enölisted in a Global Transaction. During enlisting, the transactional brnach isn't known. It is assigned during {@link javax.transaction.xa.XAResource#start(javax.transaction.xa.Xid, int)}
34   */
35  class GlobalTransactionProxy<C extends IPhynixxConnection> extends PhynixxManagedConnectionListenerAdapter<C> implements IPhynixxManagedConnectionListener<C>, ITransactionBinding<C> {
36  
37      // private TransactionManager tmMgr = null;
38  
39      private XATransactionalBranch<C> transactionalBranch;
40  
41      GlobalTransactionProxy() {
42      }
43  
44      XATransactionalBranch<C> getGlobalTransactionalBranch() {
45          return transactionalBranch;
46      }
47  
48      /**
49       *
50       * @param transactionalBranch
51       * @return if the XATransactionalBranch has to be assigned at construction , return value cann be used to assign      */
52      GlobalTransactionProxy<C> assignTransactionalBranch( XATransactionalBranch<C> transactionalBranch ) {
53          if( this.transactionalBranch!=null) {
54              throw new IllegalStateException("transactionalBranch already assigned");
55          }
56          this.transactionalBranch=transactionalBranch;
57  
58          return this;
59      }
60  
61      @Override
62      public TransactionBindingType getTransactionBindingType() {
63          return TransactionBindingType.GlobalTransaction;
64      }
65  
66      /**
67       * releases and closes the associated TransactionalBranch, but do not close the associated resources
68       */
69      @Override
70      public void release() {
71          if (transactionalBranch != null) {
72              transactionalBranch.getManagedConnection().removeConnectionListener(this);
73              /**
74               transactionalBranch.getManagedConnection().close();
75               transactionalBranchRepository.releaseTransactionalBranch(this.getXid());
76               **/
77          }
78          this.transactionalBranch = null;
79  
80      }
81  
82      /**
83       * releases and closes the associated TransactionalBranch, and releases the associated resources
84       */
85      @Override
86      public void close() {
87          if (transactionalBranch != null) {
88              XATransactionalBranch<C> branch= this.transactionalBranch;
89              this.release();
90              branch.getManagedConnection().close();
91          }
92  
93      }
94  
95  
96      Xid getXid() {
97          if (transactionalBranch != null) {
98              return this.transactionalBranch.getXid();
99          }
100         return null;
101 
102     }
103 
104 
105     @Override
106     public IPhynixxManagedConnection<C> getConnection() {
107         return this.transactionalBranch.getManagedConnection();
108     }
109 
110 }