Package org.apache.derby.jdbc

Source Code of org.apache.derby.jdbc.ResourceAdapterImpl

/*

   Derby - Class org.apache.derby.jdbc.ResourceAdapterImpl

   Copyright 1999, 2004 The Apache Software Foundation or its licensors, as applicable.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

*/

package org.apache.derby.jdbc;

import org.apache.derby.iapi.services.info.JVMInfo;

import org.apache.derby.iapi.services.context.ContextService;
import org.apache.derby.iapi.services.monitor.ModuleControl;
import org.apache.derby.iapi.services.monitor.Monitor;
import org.apache.derby.iapi.services.sanity.SanityManager;

import org.apache.derby.iapi.jdbc.ResourceAdapter;
//import org.apache.derby.iapi.jdbc.XATransactionResource;

import org.apache.derby.iapi.error.StandardException;
import org.apache.derby.iapi.store.access.AccessFactory;
import org.apache.derby.iapi.store.access.xa.XAResourceManager;
import org.apache.derby.iapi.store.access.xa.XAXactId;


import java.util.Properties;
import java.util.Hashtable;
import java.util.Enumeration;


public class ResourceAdapterImpl
    implements ResourceAdapter, ModuleControl
{
  private boolean active;

  // the real resource manager
  private XAResourceManager rm; 

  // maps Xid to XATransationResource for run time transactions
  private Hashtable connectionTable;

  /*
   * Module control
   */

  public void boot(boolean create, Properties properties)
    throws StandardException
  {
    // we can only run on jdk1.2 or beyond with JTA and JAVA 20 extension
    // loaded.
    if (SanityManager.DEBUG) {
      if (JVMInfo.JDK_ID < 2)
        SanityManager.THROWASSERT("expected JDK ID to be 2 - is " + JVMInfo.JDK_ID);
    }

    connectionTable = new Hashtable();

    AccessFactory af =
      (AccessFactory)Monitor.findServiceModule(this, AccessFactory.MODULE);

    rm = (XAResourceManager) af.getXAResourceManager();

    active = true;
  }

  public void stop()
  {
    active = false;

    for (Enumeration e = connectionTable.elements(); e.hasMoreElements(); ) {

      XATransactionState tranState = (XATransactionState) e.nextElement();

      try {
        tranState.conn.close();
      } catch (java.sql.SQLException sqle) {
      }
    }

    active = false;
  }

  public boolean isActive()
  {
    return active;
  }

  /*
   * Resource Adapter methods
   */

  public synchronized Object findConnection(XAXactId xid) {

    return connectionTable.get(xid);
  }

  public synchronized boolean addConnection(XAXactId xid, Object conn) {
    if (connectionTable.get(xid) != null)
      return false;

    // put this into the transaction table, if the xid is already
    // present as an in-doubt transaction, we need to remove it from
    // the run time list
    connectionTable.put(xid, conn);
    return true;
  }

  public synchronized Object removeConnection(XAXactId xid) {

    return connectionTable.remove(xid);

  }


  /**
    Return the XA Resource manager to the XA Connection
   */
  public XAResourceManager getXAResourceManager()
  {
    return rm;
  }
}
TOP

Related Classes of org.apache.derby.jdbc.ResourceAdapterImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.