Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: FBXADataSource.java 56780 2011-09-25 08:52:49Z mrotteveel $
   * 
   * Firebird Open Source J2EE Connector - JDBC Driver
   *
   * Distributable under LGPL license.
   * You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * LGPL License for more details.
  *
  * This file was created by members of the firebird development team.
  * All individual contributions remain the Copyright (C) of those
  * individuals.  Contributors to this file are either listed here or
  * can be obtained from a CVS history command.
  *
  * All rights reserved.
  */
 package org.firebirdsql.ds;
 
 
 
Bare-bones implementation of javax.sql.XADataSource.

Use this class instead of the broken implementation of org.firebirdsql.pool.FBConnectionPoolDataSource.

Author(s):
Mark Rotteveel
Since:
2.2
 
 public class FBXADataSource extends FBAbstractCommonDataSource implements XADataSourceReferenceable {
 
     private volatile transient FBDataSource internalDs;
 
     public XAConnection getXAConnection() throws SQLException {
         return getXAConnection(getUser(), getPassword());
     }
 
     public XAConnection getXAConnection(String userString passwordthrows SQLException {
         if ( == null) {
             initialize();
         }
         AbstractConnection connection = (AbstractConnection.getConnection(user,
                 password);
         return new FBXAConnection(connection);
     }
 
     private void initialize() throws SQLException {
         synchronized () {
             if ( != null) {
                 return;
             }
             try {
                 GDSType gdsType = GDSType.getType(getType());
                 if (gdsType == null) {
                     gdsType = GDSFactory.getDefaultGDSType();
                 }
                 FBManagedConnectionFactory mcf = new FBManagedConnectionFactory(gdsType,
                         getConnectionProperties());
                 mcf.setDefaultConnectionManager(new XAConnectionManager());
                  = (FBDataSourcemcf.createConnectionFactory();
                 .setLogWriter(getLogWriter());
             } catch (ResourceException e) {
                 throw new FBSQLException(e);
             }
         }
     }
 
     protected void checkNotStarted() throws IllegalStateException {
         if ( != null) {
             throw new IllegalStateException(
                     "DataSource already in use. Change of this property is not allowed");
         }
     }
    private static class XAConnectionManager implements ConnectionManagerConnectionEventListener {
        private static final long serialVersionUID = 2615167799315401379L;
                ConnectionRequestInfo cxRequestInfothrows ResourceException {
            FBManagedConnection mc = (FBManagedConnection) ((FBManagedConnectionFactorymcf)
                    .createManagedConnection(nullcxRequestInfo);
            mc.setManagedEnvironment(true);
            mc.setConnectionSharing(false);
            mc.addConnectionEventListener(this);
            return mc.getConnection(nullcxRequestInfo);
        }
        // javax.resource.spi.ConnectionEventListener implementation

        
javax.resource.spi.ConnectionEventListener callback for when a ManagedConnection is closed.

Parameters:
ce contains information about the connection that has be closed
        public void connectionClosed(ConnectionEvent ce) {
            PrintWriter externalLog = ((FBManagedConnectionce.getSource()).getLogWriter();
            try {
                ((FBManagedConnectionce.getSource()).destroy();
            } catch (ResourceException e) {
                if (externalLog != null)
                    externalLog.println("Exception closing unmanaged connection: " + e);
            }
        }

        
javax.resource.spi.ConnectionEventListener callback for when a Local Transaction was rolled back within the context of a ManagedConnection.

Parameters:
ce contains information about the connection
        public void connectionErrorOccurred(ConnectionEvent ce) {
            PrintWriter externalLog = ((FBManagedConnectionce.getSource()).getLogWriter();
            try {
                ((FBManagedConnectionce.getSource()).destroy();
            } catch (ResourceException e) {
                if (externalLog != null)
                    externalLog.println("Exception closing unmanaged connection: " + e);
            }
        }
        // We are only supposed to be notified of local transactions that a
        // Connection started.
        // Not much we can do with this info...

        
Ignored event callback
        public void localTransactionStarted(ConnectionEvent event) {
        }

        
Ignored event callback
        public void localTransactionCommitted(ConnectionEvent event) {
        }

        
Ignored event callback
        public void localTransactionRolledback(ConnectionEvent event) {
        }
    }
    public Reference getReference() throws NamingException {
        Reference ref = new Reference(getClass().getName(), DataSourceFactory.class.getName(), null);
        FBAbstractCommonDataSource.updateReference(refthis);
        return ref;
    }
New to GrepCode? Check out our FAQ X