Search in sources :

Example 6 with FBConnection

use of org.firebirdsql.jdbc.FBConnection in project jaybird by FirebirdSQL.

the class TestFBManager method testDialect1_dbCreatedWithRightDialect.

@Test
public void testDialect1_dbCreatedWithRightDialect() throws Exception {
    FBManager m = createFBManager();
    m.setServer(DB_SERVER_URL);
    m.setPort(DB_SERVER_PORT);
    m.start();
    try {
        // Adding .fdb suffix to prevent conflicts with other tests if drop fails
        final String databasePath = getDatabasePath() + ".fdb";
        m.setDialect(1);
        // check create
        m.createDatabase(databasePath, DB_USER, DB_PASSWORD);
        try {
            FBConnection connection = (FBConnection) DriverManager.getConnection(getUrl() + ".fdb", getDefaultPropertiesForConnection());
            try {
                final FbDatabase currentDatabase = connection.getGDSHelper().getCurrentDatabase();
                assertEquals("Unexpected database dialect", 1, currentDatabase.getDatabaseDialect());
            } finally {
                connection.close();
            }
        } finally {
            m.dropDatabase(databasePath, DB_USER, DB_PASSWORD);
        }
    } finally {
        m.stop();
    }
}
Also used : FBConnection(org.firebirdsql.jdbc.FBConnection) FbDatabase(org.firebirdsql.gds.ng.FbDatabase) Test(org.junit.Test)

Example 7 with FBConnection

use of org.firebirdsql.jdbc.FBConnection in project jaybird by FirebirdSQL.

the class FBManagedConnection method disassociateConnections.

/**
 * Disassociate connections from current managed connection.
 */
private void disassociateConnections() throws ResourceException {
    SQLExceptionChainBuilder<SQLException> chain = new SQLExceptionChainBuilder<>();
    // Iterate over copy of list as connection.close() will remove connection
    List<FBConnection> connectionHandleCopy = new ArrayList<>(connectionHandles);
    for (FBConnection connection : connectionHandleCopy) {
        try {
            connection.close();
        } catch (SQLException sqlex) {
            chain.append(sqlex);
        }
    }
    if (chain.hasException())
        throw new FBResourceException(chain.getException());
}
Also used : SQLExceptionChainBuilder(org.firebirdsql.util.SQLExceptionChainBuilder) SQLException(java.sql.SQLException) FBConnection(org.firebirdsql.jdbc.FBConnection) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 8 with FBConnection

use of org.firebirdsql.jdbc.FBConnection in project jaybird by FirebirdSQL.

the class FBManagedConnection method associateConnection.

/**
 * Used by the container to change the association of an application-level
 * connection handle with a ManagedConneciton instance. The container should
 * find the right ManagedConnection instance and call the
 * associateConnection method.
 * <P>
 * The resource adapter is required to implement the associateConnection
 * method. The method implementation for a ManagedConnection should
 * dissociate the connection handle (passed as a parameter) from its
 * currently associated ManagedConnection and associate the new connection
 * handle with itself.
 *
 * @param connection
 *            Application-level connection handle
 * @throws ResourceException
 *             Failed to associate the connection handle with this
 *             ManagedConnection instance
 * @throws javax.resource.spi.IllegalStateException
 *             Illegal state for invoking this method
 * @throws ResourceAdapterInternalException
 *             Resource adapter internal error condition
 */
public void associateConnection(Object connection) throws ResourceException {
    if (!connectionSharing)
        disassociateConnections();
    try {
        final FBConnection abstractConnection = (FBConnection) connection;
        abstractConnection.setManagedConnection(this);
        connectionHandles.add(abstractConnection);
    } catch (ClassCastException cce) {
        throw new FBResourceException("invalid connection supplied to associateConnection.", cce);
    }
}
Also used : FBConnection(org.firebirdsql.jdbc.FBConnection)

Example 9 with FBConnection

use of org.firebirdsql.jdbc.FBConnection in project jaybird by FirebirdSQL.

the class FBManagedConnection method getConnection.

/**
 * Creates a new connection handle for the underlying physical connection
 * represented by the <code>ManagedConnection</code> instance. This
 * connection handle is used by the application code to refer to the
 * underlying physical connection. A connection handle is tied to its
 * <code>ManagedConnection</code> instance in a resource adapter
 * implementation specific way.
 * <P>
 *
 * The <code>ManagedConnection</code> uses the Subject and additional
 * <code>ConnectionRequestInfo</code> (which is specific to resource
 * adapter and opaque to application server) to set the state of the
 * physical connection.
 *
 * @param subject
 *            security context as JAAS subject
 * @param cri
 *            ConnectionRequestInfo instance
 * @return generic <code>Object</code> instance representing the
 *         connection handle. For CCI, the connection handle created by a
 *         <code>ManagedConnection</code> instance is of the type
 *         <code>javax.resource.cci.Connection</code>.
 * @throws ResourceException
 *             generic exception if operation fails
 * @throws ResourceAdapterInternalException
 *             resource adapter internal error condition
 * @throws javax.resource.spi.SecurityException
 *             security related error condition
 * @throws CommException
 *             failed communication with EIS instance
 * @throws EISSystemException
 *             internal error condition in EIS instance - used if EIS
 *             instance is involved in setting state of
 *             <code>ManagedConnection</code>
 */
public Object getConnection(Subject subject, ConnectionRequestInfo cri) throws ResourceException {
    if (!matches(subject, cri))
        throw new FBResourceException("Incompatible subject or ConnectionRequestInfo in getConnection!");
    if (!connectionSharing)
        disassociateConnections();
    FBConnection c = mcf.newConnection(this);
    try {
        if (unnotifiedWarnings != null) {
            c.addWarning(unnotifiedWarnings);
            unnotifiedWarnings = null;
        }
        c.setManagedEnvironment(isManagedEnvironment());
        connectionHandles.add(c);
        return c;
    } catch (SQLException ex) {
        throw new FBResourceException(ex);
    }
}
Also used : FBConnection(org.firebirdsql.jdbc.FBConnection) SQLException(java.sql.SQLException)

Example 10 with FBConnection

use of org.firebirdsql.jdbc.FBConnection in project jaybird by FirebirdSQL.

the class TestGDSHelper method testCompareToVersion.

public void testCompareToVersion() throws Exception {
    FBConnection connection = (FBConnection) getConnectionViaDriverManager();
    GDSHelper gdsHelper = connection.getGDSHelper();
    final int actualMajor = gdsHelper.getDatabaseProductMajorVersion();
    final int actualMinor = gdsHelper.getDatabaseProductMinorVersion();
    try {
        assertEquals("compareToVersion should return 0 for identical versions", 0, gdsHelper.compareToVersion(actualMajor, actualMinor));
        assertTrue("compareToVersion should return negative value for comparison with same major and bigger minor", gdsHelper.compareToVersion(actualMajor, actualMinor + 1) < 0);
        assertTrue("compareToVersion should return positive value for comparison with same major and smaller minor", gdsHelper.compareToVersion(actualMajor, actualMinor - 1) > 0);
        assertTrue("compareToVersion should return negative value for comparison with bigger major", gdsHelper.compareToVersion(actualMajor + 1, 0) < 0);
        assertTrue("compareToVersion should return positive value for comparison with smaller major", gdsHelper.compareToVersion(actualMajor - 1, 999999) > 0);
    } finally {
        connection.close();
    }
}
Also used : FBConnection(org.firebirdsql.jdbc.FBConnection)

Aggregations

FBConnection (org.firebirdsql.jdbc.FBConnection)15 Test (org.junit.Test)10 FbDatabase (org.firebirdsql.gds.ng.FbDatabase)6 DataSource (javax.sql.DataSource)5 LocalTransaction (javax.resource.spi.LocalTransaction)4 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 GDSHelper (org.firebirdsql.gds.impl.GDSHelper)2 Statement (java.sql.Statement)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 TransactionParameterBuffer (org.firebirdsql.gds.TransactionParameterBuffer)1 FbTransaction (org.firebirdsql.gds.ng.FbTransaction)1 SQLExceptionChainBuilder (org.firebirdsql.util.SQLExceptionChainBuilder)1