Search in sources :

Example 1 with FBSQLException

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

the class FBXADataSource method initialize.

private void initialize() throws SQLException {
    synchronized (lock) {
        if (internalDs != null) {
            return;
        }
        try {
            GDSType gdsType = GDSType.getType(getType());
            if (gdsType == null) {
                gdsType = GDSFactory.getDefaultGDSType();
            }
            FBManagedConnectionFactory mcf = new FBManagedConnectionFactory(gdsType, getConnectionProperties());
            mcf.setDefaultConnectionManager(new XAConnectionManager());
            internalDs = (FBDataSource) mcf.createConnectionFactory();
            internalDs.setLogWriter(getLogWriter());
        } catch (ResourceException e) {
            throw new FBSQLException(e);
        }
    }
}
Also used : FBManagedConnectionFactory(org.firebirdsql.jca.FBManagedConnectionFactory) ResourceException(javax.resource.ResourceException) FBSQLException(org.firebirdsql.jdbc.FBSQLException) GDSType(org.firebirdsql.gds.impl.GDSType)

Example 2 with FBSQLException

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

the class PooledConnectionHandler method invoke.

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    // Methods from object
    if (method.equals(TO_STRING)) {
        return "Proxy for " + connection;
    }
    if (method.equals(EQUALS)) {
        // nulled after closing
        return proxy == args[0];
    }
    if (method.equals(HASH_CODE)) {
        // nulled after closing
        return System.identityHashCode(proxy);
    }
    // Other methods from object
    if (method.getDeclaringClass().equals(Object.class)) {
        try {
            return method.invoke(connection, args);
        } catch (InvocationTargetException e) {
            throw e.getTargetException();
        }
    }
    // Methods from Connection
    if (method.equals(CONNECTION_IS_CLOSED)) {
        return isClosed();
    }
    if (isClosed() && !method.equals(CONNECTION_CLOSE)) {
        String message = forcedClose ? FORCIBLY_CLOSED_MESSAGE : CLOSED_MESSAGE;
        throw new FBSQLException(message, SQLStateConstants.SQL_STATE_CONNECTION_CLOSED);
    }
    try {
        // Life cycle methods
        if (method.equals(CONNECTION_CLOSE)) {
            if (!isClosed()) {
                handleClose(true);
            }
            return null;
        }
        if (method.getDeclaringClass().equals(Connection.class) && STATEMENT_CREATION_METHOD_NAMES.contains(method.getName())) {
            Statement pstmt = (Statement) method.invoke(connection, args);
            StatementHandler stmtHandler = new StatementHandler(this, pstmt);
            openStatements.add(stmtHandler);
            return stmtHandler.getProxy();
        }
        // All other methods
        return method.invoke(connection, args);
    } catch (InvocationTargetException ite) {
        Throwable inner = ite.getTargetException();
        if (inner instanceof SQLException) {
            owner.fireConnectionError((SQLException) inner);
        }
        throw inner;
    } catch (SQLException se) {
        owner.fireConnectionError(se);
        throw se;
    }
}
Also used : SQLException(java.sql.SQLException) FBSQLException(org.firebirdsql.jdbc.FBSQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) FirebirdConnection(org.firebirdsql.jdbc.FirebirdConnection) FBSQLException(org.firebirdsql.jdbc.FBSQLException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with FBSQLException

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

the class TestPooledConnectionHandlerMock method testException_Notify.

/**
 * Calling a Connection method on an open proxy should notify the owner of
 * the occurrence of an exception.
 *
 * @throws SQLException
 */
@Test
public void testException_Notify() throws SQLException {
    final FirebirdConnection physicalConnection = context.mock(FirebirdConnection.class);
    final FBPooledConnection pooled = context.mock(FBPooledConnection.class);
    final PooledConnectionHandler handler = new PooledConnectionHandler(physicalConnection, pooled);
    final Sequence exceptionSequence = context.sequence("exceptionSequence");
    context.checking(new Expectations() {

        {
            SQLException sqle = new FBSQLException("Mock Exception");
            oneOf(physicalConnection).clearWarnings();
            will(throwException(sqle));
            inSequence(exceptionSequence);
            oneOf(pooled).fireConnectionError(sqle);
            inSequence(exceptionSequence);
        }
    });
    Connection proxy = handler.getProxy();
    expectedException.expect(SQLException.class);
    proxy.clearWarnings();
}
Also used : Expectations(org.jmock.Expectations) SQLException(java.sql.SQLException) FBSQLException(org.firebirdsql.jdbc.FBSQLException) Connection(java.sql.Connection) FirebirdConnection(org.firebirdsql.jdbc.FirebirdConnection) Sequence(org.jmock.Sequence) FBSQLException(org.firebirdsql.jdbc.FBSQLException) FirebirdConnection(org.firebirdsql.jdbc.FirebirdConnection) Test(org.junit.Test)

Example 4 with FBSQLException

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

the class TestFBPooledConnectionMock method testFatalExceptionFiresConnectionErrorOccurred.

/**
 * A fatal SQLException thrown during getConnection should fire a
 * connectionErrorOccurred event.
 *
 * @throws SQLException
 */
@Test
public void testFatalExceptionFiresConnectionErrorOccurred() throws SQLException {
    final Connection physical = context.mock(Connection.class);
    final FBPooledConnection pooled = new FBPooledConnection(physical);
    final ConnectionEventListener cel = context.mock(ConnectionEventListener.class);
    pooled.addConnectionEventListener(cel);
    context.checking(new Expectations() {

        {
            oneOf(physical).setAutoCommit(true);
            will(throwException(new FBSQLException("Mock Exception", SQLStateConstants.SQL_STATE_CONNECTION_FAILURE)));
            oneOf(cel).connectionErrorOccurred(with(new ConnectionEventMatcher(pooled, aNonNull(SQLException.class))));
        }
    });
    expectedException.expect(SQLException.class);
    expectedException.expect(sqlStateEquals(SQLStateConstants.SQL_STATE_CONNECTION_FAILURE));
    pooled.getConnection();
}
Also used : Expectations(org.jmock.Expectations) Connection(java.sql.Connection) FBSQLException(org.firebirdsql.jdbc.FBSQLException) ConnectionEventListener(javax.sql.ConnectionEventListener) Test(org.junit.Test)

Example 5 with FBSQLException

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

the class TestStatementHandlerMock method testException_notify.

/**
 * Calling a Statement method on an open proxy should notify the owner of
 * the occurrence of an exception.
 *
 * @throws SQLException
 */
@Test
public void testException_notify() throws SQLException {
    final PooledConnectionHandler conHandler = context.mock(PooledConnectionHandler.class);
    final Statement statement = context.mock(Statement.class);
    final StatementHandler handler = new StatementHandler(conHandler, statement);
    final Sequence exceptionSequence = context.sequence("exceptionSequence");
    context.checking(new Expectations() {

        {
            SQLException ex = new FBSQLException("Mock Exception");
            oneOf(statement).getFetchSize();
            will(throwException(ex));
            inSequence(exceptionSequence);
            oneOf(conHandler).statementErrorOccurred(handler, ex);
            inSequence(exceptionSequence);
        }
    });
    Statement proxy = handler.getProxy();
    expectedException.expect(SQLException.class);
    proxy.getFetchSize();
}
Also used : Expectations(org.jmock.Expectations) SQLException(java.sql.SQLException) FBSQLException(org.firebirdsql.jdbc.FBSQLException) Statement(java.sql.Statement) Sequence(org.jmock.Sequence) FBSQLException(org.firebirdsql.jdbc.FBSQLException) Test(org.junit.Test)

Aggregations

FBSQLException (org.firebirdsql.jdbc.FBSQLException)7 SQLException (java.sql.SQLException)4 Connection (java.sql.Connection)3 Expectations (org.jmock.Expectations)3 Test (org.junit.Test)3 Statement (java.sql.Statement)2 ResourceException (javax.resource.ResourceException)2 GDSType (org.firebirdsql.gds.impl.GDSType)2 FBManagedConnectionFactory (org.firebirdsql.jca.FBManagedConnectionFactory)2 FirebirdConnection (org.firebirdsql.jdbc.FirebirdConnection)2 Sequence (org.jmock.Sequence)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ConnectionEventListener (javax.sql.ConnectionEventListener)1