All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mchange.v2.c3p0.stmt.DoubleMaxStatementCache Maven / Gradle / Ivy

There is a newer version: 0.9.5.2_1
Show newest version
package com.mchange.v2.c3p0.stmt;

import java.sql.*;
import com.mchange.v2.async.AsynchronousRunner;

public final class DoubleMaxStatementCache extends GooGooStatementCache
{
    //MT: protected by parent's mainLock
    int max_statements;
    Deathmarch globalDeathmarch = new Deathmarch();

    int max_statements_per_connection;
    DeathmarchConnectionStatementManager dcsm;

    public DoubleMaxStatementCache(
      AsynchronousRunner blockingTaskAsyncRunner,
      AsynchronousRunner deferredStatementDestroyer,
      int max_statements,
      int max_statements_per_connection,
      boolean cancelAutomaticallyClosedStatements
    )
    {
	super( blockingTaskAsyncRunner, deferredStatementDestroyer, cancelAutomaticallyClosedStatements );
	this.max_statements = max_statements;
	this.max_statements_per_connection = max_statements_per_connection;
    }

    //called only in parent's constructor
    protected ConnectionStatementManager createConnectionStatementManager()
    { return (this.dcsm = new DeathmarchConnectionStatementManager()); }

    //all methods below called by parent only with parent's mainLock
    void addStatementToDeathmarches( Object pstmt, Connection physicalConnection )
    {
	globalDeathmarch.deathmarchStatement( pstmt );
	dcsm.getDeathmarch( physicalConnection ).deathmarchStatement( pstmt ); 
    }

    void removeStatementFromDeathmarches( Object pstmt, Connection physicalConnection )
    { 
	globalDeathmarch.undeathmarchStatement( pstmt );
	dcsm.getDeathmarch( physicalConnection ).undeathmarchStatement( pstmt ); 
    }

    boolean prepareAssimilateNewStatement(Connection pcon)
    {
	int cxn_stmt_count = dcsm.getNumStatementsForConnection( pcon );
	if (cxn_stmt_count < max_statements_per_connection) //okay... we can cache another for the connection, but how 'bout globally?
	    {
		int global_size = this.countCachedStatements();
		return (  global_size < max_statements || (global_size == max_statements && globalDeathmarch.cullNext()) );
	    }
	else //we can only cache if we can clear one from the Connection (which implies clearing one globally, so we needn't check max_statements)
	    return (cxn_stmt_count == max_statements_per_connection && dcsm.getDeathmarch( pcon ).cullNext());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy