KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > jdbc > xa > XAManagedConnection


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.resource.adapter.jdbc.xa;
23
24 import java.sql.SQLException JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import javax.resource.ResourceException JavaDoc;
28 import javax.resource.spi.LocalTransaction JavaDoc;
29 import javax.sql.XAConnection JavaDoc;
30 import javax.transaction.xa.XAException JavaDoc;
31 import javax.transaction.xa.XAResource JavaDoc;
32 import javax.transaction.xa.Xid JavaDoc;
33
34 import org.jboss.resource.JBossResourceException;
35 import org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection;
36
37 /**
38  * XAManagedConnection
39  *
40  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks </a>
41  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
42  * @author <a HREF="weston.price@jboss.com">Weston Price</a>
43  * @version $Revision: 57232 $
44  */

45 public class XAManagedConnection extends BaseWrapperManagedConnection implements XAResource JavaDoc
46 {
47    protected final XAConnection JavaDoc xaConnection;
48
49    protected final XAResource JavaDoc xaResource;
50
51    protected Xid JavaDoc currentXid;
52
53    public XAManagedConnection(XAManagedConnectionFactory mcf, XAConnection JavaDoc xaConnection, Properties JavaDoc props,
54          int transactionIsolation, int psCacheSize) throws SQLException JavaDoc
55    {
56       super(mcf, xaConnection.getConnection(), props, transactionIsolation, psCacheSize);
57       this.xaConnection = xaConnection;
58       xaConnection.addConnectionEventListener(new javax.sql.ConnectionEventListener JavaDoc()
59       {
60          public void connectionClosed(javax.sql.ConnectionEvent JavaDoc ce)
61          {
62             //only we can do this, ignore
63
}
64
65          public void connectionErrorOccurred(javax.sql.ConnectionEvent JavaDoc ce)
66          {
67             SQLException JavaDoc ex = ce.getSQLException();
68             broadcastConnectionError(ex);
69          }
70       });
71       this.xaResource = xaConnection.getXAResource();
72    }
73    
74    protected void broadcastConnectionError(SQLException JavaDoc e)
75    {
76       super.broadcastConnectionError(e);
77    }
78
79    public LocalTransaction JavaDoc getLocalTransaction() throws ResourceException JavaDoc
80    {
81       throw new JBossResourceException("xa tx only!");
82    }
83
84    public XAResource JavaDoc getXAResource() throws ResourceException JavaDoc
85    {
86       return this;
87    }
88
89    public void destroy() throws ResourceException JavaDoc
90    {
91       try
92       {
93          super.destroy();
94       }
95       finally
96       {
97          try
98          {
99             xaConnection.close();
100          }
101          catch (SQLException JavaDoc e)
102          {
103             checkException(e);
104          }
105       }
106    }
107
108    public void start(Xid JavaDoc xid, int flags) throws XAException JavaDoc
109    {
110       try
111       {
112          checkState();
113       }
114       catch (SQLException JavaDoc e)
115       {
116          getLog().warn("Error setting state ", e);
117       }
118       
119       try
120       {
121          xaResource.start(xid, flags);
122          
123       }catch(XAException JavaDoc e)
124       {
125          //JBAS-3336 Connections that fail in enlistment should not be returned
126
//to the pool
127
if(isFailedXA(e.errorCode))
128          {
129             broadcastConnectionError(e);
130          }
131          
132          throw e;
133       }
134       
135       synchronized (stateLock)
136       {
137          currentXid = xid;
138          inManagedTransaction = true;
139       }
140    }
141
142    public void end(Xid JavaDoc xid, int flags) throws XAException JavaDoc
143    {
144       
145       try
146       {
147          xaResource.end(xid, flags);
148          
149       }catch(XAException JavaDoc e)
150       {
151          broadcastConnectionError(e);
152          throw e;
153       }
154
155       
156       //we want to allow ending transactions that are not the current
157
//one. When one does this, inManagedTransaction is still true.
158
synchronized (stateLock)
159       {
160          if (currentXid != null && currentXid.equals(xid))
161          {
162             inManagedTransaction = false;
163             currentXid = null;
164          }
165       }
166    }
167
168    public int prepare(Xid JavaDoc xid) throws XAException JavaDoc
169    {
170       return xaResource.prepare(xid);
171    }
172
173    public void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc
174    {
175       xaResource.commit(xid, onePhase);
176    }
177
178    public void rollback(Xid JavaDoc xid) throws XAException JavaDoc
179    {
180       xaResource.rollback(xid);
181    }
182
183    public void forget(Xid JavaDoc xid) throws XAException JavaDoc
184    {
185       xaResource.forget(xid);
186    }
187
188    public Xid JavaDoc[] recover(int flag) throws XAException JavaDoc
189    {
190       return xaResource.recover(flag);
191    }
192
193    public boolean isSameRM(XAResource JavaDoc other) throws XAException JavaDoc
194    {
195       Boolean JavaDoc overrideValue = ((XAManagedConnectionFactory) mcf).getIsSameRMOverrideValue();
196       if (overrideValue != null)
197       {
198          return overrideValue.booleanValue();
199       }
200
201       // compare apples to apples
202
return (other instanceof XAManagedConnection)
203             ? xaResource.isSameRM(((XAManagedConnection) other).xaResource)
204             : xaResource.isSameRM(other);
205    }
206
207    public int getTransactionTimeout() throws XAException JavaDoc
208    {
209       return xaResource.getTransactionTimeout();
210    }
211
212    public boolean setTransactionTimeout(int seconds) throws XAException JavaDoc
213    {
214       return xaResource.setTransactionTimeout(seconds);
215    }
216    
217    private boolean isFailedXA(int errorCode)
218    {
219       
220       return (errorCode == XAException.XAER_RMERR || errorCode == XAException.XAER_RMFAIL);
221    }
222 }
223
Popular Tags