KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > jca > JdoManagedConnection


1 /**
2  * perseus/connector: this is an implementation of some JCA-related technologies
3  * (resource adapters and managers) for the ObjectWeb consortium.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: speedo@objectweb.org
21  *
22  */

23
24 package org.objectweb.speedo.jca;
25
26 import org.objectweb.speedo.api.Debug;
27 import org.objectweb.speedo.api.ExceptionHelper;
28 import org.objectweb.speedo.pm.api.ProxyManager;
29 import org.objectweb.util.monolog.api.BasicLevel;
30 import org.objectweb.util.monolog.api.Logger;
31
32 import javax.jdo.JDOException;
33 import javax.jdo.JDOFatalException;
34 import javax.jdo.Transaction;
35 import javax.resource.ResourceException JavaDoc;
36 import javax.resource.cci.Connection JavaDoc;
37 import javax.resource.spi.*;
38 import javax.security.auth.Subject JavaDoc;
39 import javax.transaction.RollbackException JavaDoc;
40 import javax.transaction.TransactionManager JavaDoc;
41 import javax.transaction.xa.XAException JavaDoc;
42 import javax.transaction.xa.XAResource JavaDoc;
43 import javax.transaction.xa.Xid JavaDoc;
44 import java.io.PrintWriter JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Iterator JavaDoc;
47
48 /**
49  * @author P. Dechamboux
50  */

51 public class JdoManagedConnection
52         implements ManagedConnection,
53         javax.resource.spi.LocalTransaction JavaDoc,
54         javax.resource.cci.LocalTransaction JavaDoc,
55         XAResource JavaDoc,
56         ManagedConnectionMetaData {
57
58     public final static String JavaDoc EIS_PRODUCT_NAME = "Speedo Resource Adapter";
59     public final static String JavaDoc EIS_PRODUCT_VERSION = "1.0";
60     public final static String JavaDoc USER_NAME = "No user name needed to use a JDO driver";
61
62     /**
63      * The logger into which traces about JdoManagedConnection are produced.
64      */

65     private Logger logger;
66
67     /**
68      * The ManagedConnectionFactory that have requested the allocation of this
69      * ManagedConnection.
70      */

71     private JdoManagedConnectionFactory mcf;
72
73     /**
74      * The logical Connections associated with this ManagedConnection. Always
75      * at least one connection except when the MC is in the pool.
76      */

77     private ArrayList JavaDoc logicalConnections = new ArrayList JavaDoc(1);
78
79     /**
80      * The ConnectionEventListener list associated with this ManagedConnection.
81      */

82     private ArrayList JavaDoc listeners = null;
83
84     /**
85      * It is set if the ManagedConnection execute within a LocalTransaction.
86      */

87     private ProxyManager localTransactionPM = null;
88
89     /**
90      * It is the current XAContext. It is not null when the ManagedConnection
91      * is used in a XA transaction through the XAResource interface.
92      */

93     public XAContext xac;
94
95     /**
96      * It is set if JDO operation occurs outside any explicit (LocalTransaction)
97      * or implicit (XAResource) transaction context.
98      */

99     private ProxyManager defaultPM = null;
100
101     protected JDOConnectionSpec cri = null;
102
103     /**
104      * Constructs a JdoManagedConnection.
105      * @param el The logger into which to produce ManagedConnection-related
106      * traces.
107      * @param fmcf The ManagedConnectionFactory that has requested the
108      * ManagedConnection creation.
109      */

110     JdoManagedConnection(Logger el, JdoManagedConnectionFactory fmcf) {
111         logger = el;
112         if (Debug.ON)
113             logger.log(BasicLevel.DEBUG,
114                        "Constructs a new JdoManagedConnection");
115         mcf = fmcf;
116         listeners = null;
117     }
118
119     /**
120      * Retrieves the right ProxyManager in according the managed connection
121      * state (in a local transaction, in a XA transaction or out of transaction)
122      *
123      * @return a ProxyManager instance (never null).
124      */

125     protected ProxyManager getProxyManager() {
126         if (localTransactionPM != null) {
127             if (Debug.ON && logger.isLoggable(BasicLevel.DEBUG)) {
128                 logger.log(BasicLevel.DEBUG, "return localTransactionPM: "
129                                              + localTransactionPM);
130             }
131             return localTransactionPM;
132         }
133         if (xac != null) {
134             if (Debug.ON && logger.isLoggable(BasicLevel.DEBUG)) {
135                 logger.log(BasicLevel.DEBUG,
136                            "xac.status= " + xac.status
137                            + "xac.xid= " + xac.xid);
138             }
139             if (xac.status != XAContext.STARTED) {
140                 throw new JDOFatalException(
141                         "XA context should have been started here: xac.status= "
142                         + xac.status + "xac.xid= " + xac.xid);
143             }
144             if (xac.pm == null) {
145                 synchronized(xac) {
146                     if (xac.pm == null) {
147                         xac.pm = (ProxyManager) mcf.pmf.getPersistenceManager();
148                         if (Debug.ON && logger.isLoggable(BasicLevel.DEBUG)) {
149                             logger.log(BasicLevel.DEBUG,
150                                     "Creates a PersistenceManager (" + xac.pm
151                                     + ") associated to the XID: " + xac.xid);
152                         }
153                     }
154                 }
155             }
156             if (!xac.synchroRegistred) {
157                 //Register the PersistenceManager on the first use
158
try {
159                     registerSynchronization();
160                 } catch (XAException JavaDoc ex) {
161                      JDOFatalException e = new JDOFatalException(
162                              "Problem while registering a synchronization.", ex);
163                     logger.log(BasicLevel.ERROR, e.getMessage(), ex);
164                     throw e;
165                 }
166             }
167             return xac.pm;
168         }
169         if (defaultPM == null) {
170             defaultPM = (ProxyManager) mcf.pmf.getPersistenceManager();
171         }
172         if (Debug.ON && logger.isLoggable(BasicLevel.DEBUG)) {
173             logger.log(BasicLevel.DEBUG,
174                        "return the default persistence manager: " + defaultPM);
175         }
176         return defaultPM;
177     }
178
179
180     /**
181      * find the XAContext matching an Xid.
182      * @param flag indicates expecting behavior:<UL>
183      * <LI>0: create the XAContext if it does not already exist</LI>
184      * <LI>1: return an existing XAContext</LI>
185      * <LI>otherwise: return an XAContext or null it is not found</LI>
186      * </UL>
187      */

188     private XAContext getXAContext(Xid JavaDoc xid, int flag)
189             throws XAException JavaDoc {
190         XAContext _xac = mcf.getXAContext(xid);
191         if (_xac == null) {
192             if (flag == 0) { //create it
193
try {
194                     _xac = mcf.createXAContext(xid);
195                 } catch (JDOException fe) {
196                     Exception JavaDoc ie = ExceptionHelper.getNested(fe);
197                     XAException JavaDoc e = new XAException JavaDoc(
198                             "Error during the creation of the XA context with the xid: "
199                             + xid + ", error message:" + ie.getMessage());
200                     logger.log(BasicLevel.ERROR, ie.getMessage(), ie);
201                     throw e;
202                 }
203             } else if (flag == 1) { // throw an exception
204
XAException JavaDoc e = new XAException JavaDoc("No XA context found for the xid=" + xid);
205                 logger.log(BasicLevel.ERROR, e.getMessage(), e);
206                 throw e;
207             } // else return null
208
}
209         return _xac;
210     }
211
212     /**
213      * Registers the ProxyManager of the current XAContext to the current
214      * transaction, if it is not already done.
215      *
216      * @throws XAException if the registering is not possible:
217      * - TM not availlable
218      * - Pr
219      */

220     private synchronized void registerSynchronization() throws XAException JavaDoc {
221         if (xac.synchroRegistred) {
222             return;
223         }
224         Transaction tx = xac.pm.currentTransaction();
225         if (tx.isActive()) {
226             logger.log(BasicLevel.WARN, "JDO Transaction started and the " +
227                     "PersistenceManager is not marked as registered to " +
228                     "the XA transaction");
229         }
230         if (Debug.ON && logger.isLoggable(BasicLevel.DEBUG)) {
231             logger.log(BasicLevel.DEBUG, "registerSynchronization");
232         }
233         TransactionManager JavaDoc tm = mcf.tm;
234         if (tm == null) {
235             String JavaDoc msg = "Impossible to register the JDO " +
236                     "container as synchronization on the current transaction: " +
237                     (tm == null
238                     ? "The transaction manager has not found with the JNDI name: "
239                     + mcf.getTransactionManagerJNDIName()
240                     : "The Transaction object of the JDO implementation does not implement javax.transaction.Synchronization");
241             logger.log(BasicLevel.ERROR, msg);
242             throw new XAException JavaDoc(msg);
243         }
244         try {
245             tm.getTransaction().registerSynchronization(xac.pm);
246             if (!tx.isActive()) {
247                 tx.begin();
248             }
249             xac.synchroRegistred = true;
250         } catch (RollbackException JavaDoc e) {
251             //The JDO container cannot be registered then the transaction
252
// must be rolledback
253
if (tx.isActive()) {
254                 tx.rollback();
255             }
256         } catch (Exception JavaDoc e) {
257             String JavaDoc msg = "Impossible to register the JDO " +
258                     "container as synchronization on the current" +
259                     " transaction: " + e.getMessage();
260             logger.log(BasicLevel.ERROR, msg, e);
261             throw new XAException JavaDoc(msg);
262         }
263     }
264
265     /**
266      * Specifies if this ManagedConnection still has an active LocalTransaction.
267      * @return true if a LocalTransaction is still active.
268      */

269     protected boolean localTransactionTerminated() {
270         return localTransactionPM == null;
271     }
272
273     /**
274      * Dissociates a Connection from the ones that are associated to this
275      * ManagedConnection.
276      * @param conn The Connection to be dissociated.
277      */

278     protected synchronized void dissociateConnection(Object JavaDoc conn) throws ResourceException JavaDoc {
279         int ind = logicalConnections.indexOf(conn);
280         if (ind == -1) {
281             throw new ResourceException JavaDoc("JDO Connector: cannot dissociate an unknown Connection:"
282                     + conn + "\n among " + logicalConnections);
283         }
284         if (Debug.ON && logger.isLoggable(BasicLevel.DEBUG)) {
285             logger.log(BasicLevel.DEBUG, "Dissociates Connection (" + ind + "): " + conn);
286         }
287         logicalConnections.remove(ind);
288         ConnectionEvent ce = new ConnectionEvent(this, ConnectionEvent.CONNECTION_CLOSED);
289         ce.setConnectionHandle(conn);
290         if (listeners != null) {
291             synchronized(this) {
292                 for (int i=0; i<listeners.size();) {
293                     ConnectionEventListener cel = (ConnectionEventListener) listeners.get(i);
294                     cel.connectionClosed(ce);
295                     if (i<listeners.size() && cel == listeners.get(i)) {
296                         i++;
297                     }// else the listeners has been removed
298
}
299             }
300         }
301     }
302
303     
304     // IMPLEMENTATION OF METHODS FROM THE (cci)ManagedConnectionMetaData INTERFACE
305

306     public String JavaDoc getEISProductName() throws ResourceException JavaDoc {
307         return EIS_PRODUCT_NAME;
308     }
309
310     public String JavaDoc getEISProductVersion() throws ResourceException JavaDoc {
311         return EIS_PRODUCT_VERSION;
312     }
313
314     public int getMaxConnections() throws ResourceException JavaDoc {
315         // TODO: analysis of ra.xml values
316
return 10;
317     }
318
319     public String JavaDoc getUserName() throws ResourceException JavaDoc {
320         return USER_NAME;
321     }
322
323
324     // IMPLEMENTATION OF METHODS FROM THE (cci)ManagedConnection INTERFACE //
325
//---------------------------------------------------------------------//
326

327     /**
328      * Delegates the creation of a Connection to the ConnectionFactory.
329      * "subject" and "info" parameters are ignored.
330      */

331     public Object JavaDoc getConnection(Subject JavaDoc subject, ConnectionRequestInfo info)
332             throws ResourceException JavaDoc {
333         JdoConnection rac = mcf.connectionFactory.createConnection();
334         rac.initialize(this);
335         associateConnection(rac);
336         return rac;
337     }
338
339     /**
340      * Cleans up the connection.
341      */

342     public void cleanup() throws ResourceException JavaDoc {
343         if (Debug.ON)
344             logger.log(BasicLevel.DEBUG, "Cleans up ManagedConnection");
345         int s = (logicalConnections == null ? 0 : logicalConnections.size());
346         if (s > 0) {
347             s -=1;
348             for(int i=s; i>=0; i--) {
349                 ((Connection JavaDoc) logicalConnections.get(i)).close();
350             }
351         }
352         if (localTransactionPM != null) {
353             localTransactionPM.close();
354             localTransactionPM = null;
355         }
356         if (defaultPM != null) {
357             defaultPM.close();
358             defaultPM = null;
359         }
360         //The specification requires to keep the XAResource associated to MC
361
}
362
363     /**
364      * Called when ManagedConnection is removed.
365      */

366     public void destroy() throws ResourceException JavaDoc {
367         cleanup();
368         logger = null;
369         mcf = null;
370         logicalConnections = null;
371         listeners = null;
372         localTransactionPM = null;
373     }
374
375     /**
376      * Associates a new Connection to this ManagedConnection. Nothing is done
377      * if it has already been associated (enforces no duplicate).
378      */

379     public synchronized void associateConnection(Object JavaDoc o) throws ResourceException JavaDoc {
380         if (Debug.ON)
381             logger.log(BasicLevel.DEBUG, "Associates Connection: " + o + " to the MC " + this);
382         if (!logicalConnections.contains(o))
383             logicalConnections.add(o);
384         else
385             logger.log(BasicLevel.WARN, "Connection: " + o
386                                         + "has already been associated: ignored!");
387     }
388
389     /**
390      * Adds a listener to the listeners list if it has not
391      * already been done. This means that duplicates are ignored.
392      * It first creates this list if needed (creation at first add).
393      */

394     public synchronized void addConnectionEventListener(
395             ConnectionEventListener listener) {
396         if (listeners == null)
397             listeners = new ArrayList JavaDoc();
398         if (Debug.ON)
399             logger.log(BasicLevel.DEBUG, "Associates ConnectionEventListener: "
400                                          + listener);
401         if (!listeners.contains(listener))
402             listeners.add(listener);
403         else
404             logger.log(BasicLevel.WARN, "ConnectionEventListener: " + listener
405                                         + "has already been associated: ignored!");
406     }
407
408     /**
409      * Removes a listener from the listeners list. If the list does
410      * not eist (null), or if this listener does not belong to this list,
411      * nothing is done.
412      */

413     public synchronized void removeConnectionEventListener(
414             ConnectionEventListener listener) {
415         if (listeners == null) {
416             logger.log(BasicLevel.WARN,
417                        "No associated ConnectionEventListener: ignored!");
418             return;
419         }
420         int ind = listeners.indexOf(listener);
421         if (Debug.ON)
422             logger.log(BasicLevel.DEBUG,
423                        "Index within the listeners: " + ind);
424         if (ind == -1)
425             logger.log(BasicLevel.WARN, "ConnectionEventListener" + listener
426                                         + "not associated here: ignored!");
427         else
428             listeners.remove(ind);
429     }
430
431     /**
432      * Retrieves an XA resource. Switches running mode to XA.
433      */

434     public synchronized XAResource JavaDoc getXAResource() throws ResourceException JavaDoc {
435         logger.log(BasicLevel.DEBUG, "getXAResource()");
436         if (localTransactionPM != null) {
437             throw new ResourceException JavaDoc(
438                     "Try switching to XA mode while running a LocalTransaction.");
439         }
440         if (defaultPM != null) {
441             defaultPM.close();
442             defaultPM = null;
443         }
444         return this;
445     }
446
447     public LocalTransaction getLocalTransaction() throws ResourceException JavaDoc {
448         return this;
449     }
450
451     public ManagedConnectionMetaData getMetaData() throws ResourceException JavaDoc {
452         return this;
453     }
454
455     /**
456      * Not supported yet.
457      */

458     public void setLogWriter(PrintWriter JavaDoc writer) throws ResourceException JavaDoc {
459         throw new ResourceException JavaDoc("JDO Connector: logging to PrintWriter not supported yet.");
460     }
461
462     /**
463      * Not supported yet.
464      */

465     public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc {
466         throw new ResourceException JavaDoc("JDO Connector: logging to PrintWriter not supported yet.");
467     }
468
469
470     // IMPLEMENTATION OF METHODS FROM THE (cci)LocalTransaction INTERFACE //
471
//--------------------------------------------------------------------//
472

473     /**
474      * Begins the LocalTransaction if it has not already been started.
475      */

476     public synchronized void begin() throws ResourceException JavaDoc {
477         if (localTransactionPM != null) {
478             throw new ResourceException JavaDoc("JDO Connector: a LocalTransaction has already begun.");
479         }
480         try {
481             if (defaultPM != null) {
482                 defaultPM.close();
483                 defaultPM = null;
484             }
485             localTransactionPM = (ProxyManager)
486                     mcf.pmf.getPersistenceManager();
487             localTransactionPM.currentTransaction().begin();
488             if (Debug.ON)
489                 logger.log(BasicLevel.DEBUG, "LocalTransaction begin - txContext: "
490                                              + localTransactionPM);
491             if (xac != null)
492                 logger.log(BasicLevel.WARN,
493                            "Begins a LocalTransaction on a ManagedConnection that runs in XA mode!");
494             ConnectionEvent ce = new ConnectionEvent(this, ConnectionEvent.LOCAL_TRANSACTION_STARTED);
495             if (listeners != null) {
496                 synchronized(this) {
497                     for (Iterator JavaDoc it = listeners.iterator(); it.hasNext();) {
498                         ((ConnectionEventListener) it.next()).localTransactionStarted(ce);
499                     }
500                 }
501             }
502         } catch (JDOException fe) {
503             ResourceException JavaDoc re = new ResourceException JavaDoc(
504                     "JDO Connector: cannot begin LocalTransaction [nested exception].");
505             re.setLinkedException(fe);
506             throw re;
507         }
508     }
509
510     /**
511      * Commits the LocalTransaction if it is active.
512      */

513     public synchronized void commit() throws ResourceException JavaDoc {
514         if (localTransactionPM == null)
515             throw new ResourceException JavaDoc("JDO Connector: no LocalTransaction has been begun.");
516         if (Debug.ON)
517             logger.log(BasicLevel.DEBUG, "LocalTransaction commit - txContext: "
518                                          + localTransactionPM);
519         try {
520             localTransactionPM.currentTransaction().commit();
521             ConnectionEvent ce = new ConnectionEvent(this, ConnectionEvent.LOCAL_TRANSACTION_COMMITTED);
522             if (listeners != null) {
523                 synchronized(this) {
524                     for (Iterator JavaDoc it = listeners.iterator(); it.hasNext();) {
525                         ((ConnectionEventListener) it.next()).localTransactionCommitted(ce);
526                     }
527                 }
528             }
529         } catch (JDOException fe) {
530             ResourceException JavaDoc re = new ResourceException JavaDoc(
531                     "JDO Connector: cannot commit LocalTransaction [nested exception].");
532             re.setLinkedException(fe);
533             throw re;
534         } finally {
535             localTransactionPM.close();
536             localTransactionPM = null;
537         }
538     }
539
540     /**
541      * Rollbacks the LocalTransaction if it is active.
542      */

543     public synchronized void rollback() throws ResourceException JavaDoc {
544         if (localTransactionPM == null)
545             throw new ResourceException JavaDoc("JDO Connector: no LocalTransaction has been begun.");
546         if (Debug.ON)
547             logger.log(BasicLevel.DEBUG, "LocalTransaction rollback - txContext: "
548                                          + localTransactionPM);
549         try {
550             localTransactionPM.currentTransaction().rollback();
551             ConnectionEvent ce = new ConnectionEvent(this, ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK);
552             if (listeners != null) {
553                 synchronized(this) {
554                     for (Iterator JavaDoc it = listeners.iterator(); it.hasNext();) {
555                         ((ConnectionEventListener) it.next()).localTransactionRolledback(ce);
556                     }
557                 }
558             }
559         } catch (JDOException fe) {
560             ResourceException JavaDoc re = new ResourceException JavaDoc(
561                     "JDO Connector: cannot rollback LocalTransaction [nested exception].");
562             re.setLinkedException(fe);
563             throw re;
564         } finally {
565             localTransactionPM.close();
566             localTransactionPM = null;
567         }
568     }
569
570
571     // IMPLEMENTATION OF METHODS FROM THE (jta)XAResource INTERFACE //
572
//--------------------------------------------------------------//
573

574     /**
575      * Assigns an actual JDO transaction context to the XAResource
576      * within the give DTP context.
577      */

578     public void start(Xid JavaDoc xid, int i) throws XAException JavaDoc {
579         try {
580             switch(i) {
581             case XAResource.TMRESUME:
582                 if (Debug.ON && logger.isLoggable(BasicLevel.DEBUG)) {
583                     logger.log(BasicLevel.DEBUG, "start(" + xid + ", TMRESUME)");
584                 }
585                 xac = getXAContext(xid, 1);
586                 if (xac.pm != null) {
587                     mcf.pmf.bindPM2Thread(xac.pm);
588                 }
589                 break;
590             case XAResource.TMJOIN:
591                 if (Debug.ON && logger.isLoggable(BasicLevel.DEBUG)) {
592                     logger.log(BasicLevel.DEBUG, "start(" + xid + ", TMJOIN)");
593                 }
594                 xac = getXAContext(xid, 1);
595                 break;
596             case XAResource.TMNOFLAGS:
597                 if (Debug.ON && logger.isLoggable(BasicLevel.DEBUG)) {
598                     logger.log(BasicLevel.DEBUG, "start(" + xid + ", TMNOFLAGS)");
599                 }
600                 xac = getXAContext(xid, 0);
601                 break;
602             default:
603                 String JavaDoc msg = "Unexpected flag: " + i;
604                 logger.log(BasicLevel.ERROR, msg);
605                 throw new XAException JavaDoc(msg);
606             }
607             xac.status = XAContext.STARTED;
608         } catch (XAException JavaDoc e) {
609             logger.log(BasicLevel.ERROR,
610                     "Error in the Jdo XAResource starting (" + i + ")", e);
611             throw e;
612         } catch (RuntimeException JavaDoc e) {
613             logger.log(BasicLevel.ERROR,
614                     "Error in the Jdo XAResource starting (" + i + ")", e);
615             throw e;
616         }
617     }
618
619     /**
620      * Unbind this instance to the PersistenceManager.
621      */

622     public void end(Xid JavaDoc xid, int i) throws XAException JavaDoc {
623         if (Debug.ON && logger.isLoggable(BasicLevel.DEBUG)) {
624             logger.log(BasicLevel.DEBUG, "xid=" + xid);
625         }
626         XAContext _xac = getXAContext(xid, 2);
627         if (_xac != null) {
628             _xac.status = XAContext.ENDED;
629             if (xac == _xac) {
630                 xac = null;
631             }
632         } // else the XAcontext has been committed or rolled back before the end
633
}
634
635     /**
636      * Used by JTA in order to verify that it has not already registered a
637      * XAResource to manage this transaction context from this RM. If it is the
638      * case, both XAResource are warned of this fact (endWithNoSameRM = false).
639      * This means that these XAResource will be released at "end" time.
640      * @param resource The resource to be compared against this one wrt RM.
641      */

642     public boolean isSameRM(XAResource JavaDoc resource) throws XAException JavaDoc {
643         boolean res = (resource instanceof JdoManagedConnection)
644                 && ((JdoManagedConnection) resource).mcf == mcf;
645         if (Debug.ON && logger.isLoggable(BasicLevel.DEBUG)) {
646             logger.log(BasicLevel.DEBUG, "isSameRM(" + resource + "): " + res);
647         }
648         return res;
649     }
650
651     // Management of transaction termination: prepare, commit, rollback
652

653     /**
654      * Prepares the underlying JdoTxContext (prepare phase of the 2PC).
655      */

656     public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
657         if (Debug.ON && logger.isLoggable(BasicLevel.DEBUG)) {
658             logger.log(BasicLevel.DEBUG, "prepare(" + xid + ")");
659         }
660         XAContext _xac = getXAContext(xid, 1);
661         if (Debug.ON && logger.isLoggable(BasicLevel.DEBUG)) {
662             if (_xac.pm == null) {
663                 logger.log(BasicLevel.DEBUG, "prepare(" + xid
664                         + "): no PersistenceManager");
665             } else {
666                 logger.log(BasicLevel.DEBUG, "prepare(" + xid
667                         + "): speedo Tx status="
668                         + ((org.objectweb.speedo.workingset.api.Transaction)
669                         _xac.pm.currentTransaction()).getStatus());
670             }
671         }
672         return XAResource.XA_OK;
673     }
674
675     /**
676      * Unbind the PersistenceManager to the xid. The real commit is done by the
677      * PersistenceManager registered as a Synchronization.
678      */

679     public void commit(Xid JavaDoc xid, boolean b) throws XAException JavaDoc {
680         if (Debug.ON && logger.isLoggable(BasicLevel.DEBUG)) {
681             logger.log(BasicLevel.DEBUG, "commit(" + xid + ", 1PC: " + b + ")");
682         }
683         XAContext _xac = mcf.releaseXAContext(xid, true);
684         if (xac == _xac) {
685             this.xac = null;
686         }
687     }
688
689     /**
690      * Unbind the PersistenceManager to the xid. The real commit is done by the
691      * PersistenceManager registered as a Synchronization.
692      */

693     public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
694         if (Debug.ON && logger.isLoggable(BasicLevel.DEBUG)) {
695             logger.log(BasicLevel.DEBUG, "rollback(" + xid + ")");
696         }
697         XAContext _xac = mcf.releaseXAContext(xid, true);
698         if (xac == _xac) {
699             this.xac = null;
700         }
701     }
702
703     public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
704         XAContext _xac = mcf.releaseXAContext(xid, false);
705         if (xac == _xac) {
706             this.xac = null;
707         }
708     }
709
710     /**
711      * Gets the Xid of distributed transactions to be recovered from the JDO
712      * manager.
713      */

714     public Xid JavaDoc[] recover(int i) throws XAException JavaDoc {
715         return new Xid JavaDoc[0];
716     }
717
718     public int getTransactionTimeout() throws XAException JavaDoc {
719         return 0;
720     }
721
722     public boolean setTransactionTimeout(int i) throws XAException JavaDoc {
723         return false;
724     }
725
726 }
Popular Tags