Search in sources :

Example 1 with GenericPreparedStatement

use of org.apache.derby.impl.sql.GenericPreparedStatement in project derby by apache.

the class GenericLanguageConnectionContext method lookupStatement.

/**
 * See if a given statement has already been compiled for this user, and
 * if so use its prepared statement. Returns null if not found.
 *
 * @exception StandardException thrown if lookup goes wrong.
 * @return the prepared statement for the given string, null
 *  if none was found.
 */
public PreparedStatement lookupStatement(GenericStatement statement) throws StandardException {
    CacheManager statementCache = getLanguageConnectionFactory().getStatementCache();
    if (statementCache == null)
        return null;
    // statement caching disable when in DDL mode
    if (dataDictionaryInWriteMode()) {
        return null;
    }
    Cacheable cachedItem = statementCache.find(statement);
    CachedStatement cs = (CachedStatement) cachedItem;
    GenericPreparedStatement ps = cs.getPreparedStatement();
    synchronized (ps) {
        if (ps.upToDate()) {
            GeneratedClass ac = ps.getActivationClass();
            // Check to see if the statement was prepared before some change
            // in the class loading set. If this is the case then force it to be invalid
            int currentClasses = getLanguageConnectionFactory().getClassFactory().getClassLoaderVersion();
            if (ac.getClassLoaderVersion() != currentClasses) {
                ps.makeInvalid(DependencyManager.INTERNAL_RECOMPILE_REQUEST, this);
            }
        // note that the PreparedStatement is not kept in the cache. This is because
        // having items kept in the cache that ultimately are held onto by
        // user code is impossible to manage. E.g. an open ResultSet would hold onto
        // a PreparedStatement (through its activation) and the user can allow
        // this object to be garbage collected. Pushing a context stack is impossible
        // in garbage collection as it may deadlock with the open connection and
        // the context manager assumes a singel current thread per context stack
        }
    }
    statementCache.release(cachedItem);
    return ps;
}
Also used : GeneratedClass(org.apache.derby.iapi.services.loader.GeneratedClass) Cacheable(org.apache.derby.iapi.services.cache.Cacheable) CacheManager(org.apache.derby.iapi.services.cache.CacheManager) GenericPreparedStatement(org.apache.derby.impl.sql.GenericPreparedStatement)

Example 2 with GenericPreparedStatement

use of org.apache.derby.impl.sql.GenericPreparedStatement in project derby by apache.

the class ConstraintCharacteristicsTest method testAlterConstraintInvalidation.

/**
 * Check that altering constraint characteristics invalidates prepared
 * statements.
 * @throws SQLException
 */
public void testAlterConstraintInvalidation() throws SQLException {
    if (usingDerbyNetClient()) {
        // Skip, since we need to see inside an embedded connection here
        return;
    }
    final Connection c = getConnection();
    final Statement s = createStatement();
    s.executeUpdate("create table t(i int, constraint c primary key(i))");
    final PreparedStatement ps = c.prepareStatement("insert into t values 3");
    ps.execute();
    s.executeUpdate("alter table t alter constraint c enforced ");
    final LanguageConnectionContext lcc = getLCC(c);
    final GenericPreparedStatement derbyPs = (GenericPreparedStatement) lcc.getLastActivation().getPreparedStatement();
    assertFalse(derbyPs.isValid());
    rollback();
}
Also used : LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) GenericPreparedStatement(org.apache.derby.impl.sql.GenericPreparedStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) EmbedConnection(org.apache.derby.impl.jdbc.EmbedConnection) GenericPreparedStatement(org.apache.derby.impl.sql.GenericPreparedStatement) PreparedStatement(java.sql.PreparedStatement) GenericPreparedStatement(org.apache.derby.impl.sql.GenericPreparedStatement)

Example 3 with GenericPreparedStatement

use of org.apache.derby.impl.sql.GenericPreparedStatement in project derby by apache.

the class CachedStatement method setIdentity.

/**
 */
public Cacheable setIdentity(Object key) {
    identity = key;
    ps = new GenericPreparedStatement((GenericStatement) key);
    ps.setCacheHolder(this);
    return this;
}
Also used : GenericStatement(org.apache.derby.impl.sql.GenericStatement) GenericPreparedStatement(org.apache.derby.impl.sql.GenericPreparedStatement)

Aggregations

GenericPreparedStatement (org.apache.derby.impl.sql.GenericPreparedStatement)3 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 Statement (java.sql.Statement)1 XAConnection (javax.sql.XAConnection)1 CacheManager (org.apache.derby.iapi.services.cache.CacheManager)1 Cacheable (org.apache.derby.iapi.services.cache.Cacheable)1 GeneratedClass (org.apache.derby.iapi.services.loader.GeneratedClass)1 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)1 EmbedConnection (org.apache.derby.impl.jdbc.EmbedConnection)1 GenericStatement (org.apache.derby.impl.sql.GenericStatement)1