Search in sources :

Example 6 with EmbedConnection

use of org.apache.derby.impl.jdbc.EmbedConnection in project derby by apache.

the class EmbedXAResource method returnConnectionToResource.

/**
 * Return an underlying connection object back to its XAResource
 * if possible. If not close the connection.
 * @param tranState
 * @param xid_im
 */
void returnConnectionToResource(XATransactionState tranState, XAXactId xid_im) {
    removeXATransaction(xid_im);
    synchronized (tranState) {
        // global transaction is over.
        tranState.associationState = XATransactionState.TC_COMPLETED;
        tranState.notifyAll();
        EmbedConnection conn = tranState.conn;
        // or can it be returned to its original resource?
        if ((tranState.creatingResource.con.realConnection == conn) || (tranState.creatingResource.con.realConnection == null)) {
            tranState.creatingResource.con.realConnection = conn;
            BrokeredConnection handle = tranState.creatingResource.con.currentConnectionHandle;
            conn.setApplicationConnection(handle);
            if (handle != null) {
                try {
                    handle.setState(true);
                } catch (SQLException sqle) {
                    // couldn't reset the connection
                    closeUnusedConnection(tranState.conn);
                    tranState.creatingResource.con.realConnection = null;
                }
            }
            return;
        }
    }
    // nowhere to place it, close it.
    closeUnusedConnection(tranState.conn);
}
Also used : SQLException(java.sql.SQLException) BrokeredConnection(org.apache.derby.iapi.jdbc.BrokeredConnection) EmbedConnection(org.apache.derby.impl.jdbc.EmbedConnection)

Example 7 with EmbedConnection

use of org.apache.derby.impl.jdbc.EmbedConnection in project derby by apache.

the class BaseJDBCTestCase method checkEstimatedRowCount.

/**
 * Return estimated row count for runtime statistics.
 * Requires caller first turned on RuntimeStatistics, executed a query and closed the ResultSet.
 *
 * For client calls we just return as we can't find out this information.
 * @param conn
 * @param expectedCount
 * @throws SQLException
 */
public static void checkEstimatedRowCount(Connection conn, double expectedCount) throws SQLException {
    if (!(conn instanceof EmbedConnection)) {
        return;
    }
    EmbedConnection econn = (EmbedConnection) conn;
    LanguageConnectionContext lcc = (LanguageConnectionContext) getLanguageConnectionContext(econn);
    RunTimeStatistics rts = lcc.getRunTimeStatisticsObject();
    assertNotNull(" RuntimeStatistics is null. Did you call SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(1)?", rts);
    assertEquals((long) expectedCount, (long) rts.getEstimatedRowCount());
}
Also used : RunTimeStatistics(org.apache.derby.iapi.sql.execute.RunTimeStatistics) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) EmbedConnection(org.apache.derby.impl.jdbc.EmbedConnection)

Example 8 with EmbedConnection

use of org.apache.derby.impl.jdbc.EmbedConnection in project derby by apache.

the class RawDBReader method createViews.

/**
 * Create table functions and views on corrupt user tables. These objects
 * are created in the healthy database. Write the recovery
 * script.
 */
private void createViews(Connection conn, String recoveryScriptName, String controlSchema, String schemaPrefix, String corruptDBLocation, String encryptionAttributes, String dbo, String dboPassword) throws SQLException {
    File recoveryScript = new File(recoveryScriptName);
    PrintWriter scriptWriter = null;
    try {
        scriptWriter = new PrintWriter(recoveryScript);
    } catch (Exception e) {
        throw wrap(e);
    }
    String localDBName = ((EmbedConnection) conn).getDBName();
    scriptWriter.println("connect 'jdbc:derby:" + localDBName + "';\n");
    PreparedStatement ps = prepareStatement(conn, "select s.schemaName, t.tableName, g.conglomerateNumber, c.columnName, c.columnNumber, c.columnDatatype\n" + "from " + controlSchema + ".sysschemas s,\n" + controlSchema + ".systables t,\n" + controlSchema + ".sysconglomerates g,\n" + controlSchema + ".syscolumns c\n" + "where s.schemaName not like 'SYS%' and schemaName != 'NULLID' and schemaName != 'SQLJ'\n" + "and s.schemaID = t.schemaID\n" + "and t.tableID = g.tableID and not g.isindex\n" + "and t.tableID = c.referenceID\n" + "order by s.schemaName, t.tableName, c.columnNumber");
    ResultSet rs = ps.executeQuery();
    ArrayList<String> columnNames = new ArrayList<String>();
    ArrayList<TypeDescriptor> columnTypes = new ArrayList<TypeDescriptor>();
    String corruptSchemaName = null;
    String corruptTableName = null;
    String schemaName = null;
    String tableName = null;
    long conglomerateNumber = -1L;
    while (rs.next()) {
        int col = 1;
        String currentCorruptSchemaName = rs.getString(col++);
        String currentCorruptTableName = rs.getString(col++);
        if (!currentCorruptSchemaName.equals(corruptSchemaName)) {
            scriptWriter.println("create schema " + IdUtil.normalToDelimited(currentCorruptSchemaName) + ";\n");
        }
        String newSchemaName = makeSchemaName(schemaPrefix, currentCorruptSchemaName);
        String newTableName = IdUtil.normalToDelimited(currentCorruptTableName);
        if (schemaName != null) {
            if (!schemaName.equals(newSchemaName) || !tableName.equals(newTableName)) {
                createView(conn, scriptWriter, controlSchema, corruptSchemaName, corruptTableName, schemaName, tableName, conglomerateNumber, columnNames, columnTypes, corruptDBLocation, encryptionAttributes, dbo, dboPassword);
                columnNames.clear();
                columnTypes.clear();
            }
        }
        corruptSchemaName = currentCorruptSchemaName;
        corruptTableName = currentCorruptTableName;
        schemaName = newSchemaName;
        tableName = newTableName;
        conglomerateNumber = rs.getLong(col++);
        columnNames.add(normalizeColumnName(rs.getString(col++)));
        // only need the column number to order the results
        col++;
        columnTypes.add((TypeDescriptor) rs.getObject(col++));
    }
    // create last view
    if (schemaName != null) {
        createView(conn, scriptWriter, controlSchema, corruptSchemaName, corruptTableName, schemaName, tableName, conglomerateNumber, columnNames, columnTypes, corruptDBLocation, encryptionAttributes, dbo, dboPassword);
    }
    rs.close();
    ps.close();
    scriptWriter.flush();
    scriptWriter.close();
}
Also used : ArrayList(java.util.ArrayList) EmbedConnection(org.apache.derby.impl.jdbc.EmbedConnection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) SQLException(java.sql.SQLException) TypeDescriptor(org.apache.derby.catalog.TypeDescriptor) ResultSet(java.sql.ResultSet) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

EmbedConnection (org.apache.derby.impl.jdbc.EmbedConnection)8 Connection (java.sql.Connection)4 BrokeredConnection (org.apache.derby.iapi.jdbc.BrokeredConnection)3 AccessControlException (java.security.AccessControlException)2 SQLException (java.sql.SQLException)2 EngineConnection (org.apache.derby.iapi.jdbc.EngineConnection)2 File (java.io.File)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 TypeDescriptor (org.apache.derby.catalog.TypeDescriptor)1 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)1 RunTimeStatistics (org.apache.derby.iapi.sql.execute.RunTimeStatistics)1