簡體   English   中英

oracle.jdbc.pool.OracleDataSource為每個新連接運行一個命令

[英]oracle.jdbc.pool.OracleDataSource run a command for every new connection

我想對來自特定oracle.jdbc.pool.OracleDataSource連接池的每個新連接執行ALTER SESSION命令。 有沒有辦法做到這一點?

如果要部署到應用程序服務器,請檢查配置選項,在許多情況下,您可以在其中配置一些“初始SQL”。

如果您不能這樣做,請在應用程序級別使用自定義數據源包裝oracle.jdbc.pool.OracleDataSource ,然后在其余應用程序中使用該自定義數據源。 就像是:

public class InitialSqlDataSource implements DataSource {
    private DataSource delegate;
    private String initialSql;


    public InitialSqlDataSource(DataSource delegate, String initialSql) {
        this.delegate = delegate;
        this.initialSql = initialSql;
    }

    public void setDelegate(DataSource delegate) {
        this.delegate = delegate;
    }

    public void setInitialSql(String initialSql) {
        this.initialSql = initialSql;
    }

    @Override
    public Connection getConnection() {
        Connection connection = delegate.getConnection();
        if (connection != null && isNew(connection)) {
            issueInitialSql(connection);
        }
        return connection;
    }

    protected boolean isNew(Connection connection) {
        // Logic to find out if connection is a new one
        // or a previously pooled connection. In the worst
        // case you can approximate this logic by always returning 
        // true. This will issue the initial SQL on every retrieval 
        // of a connection from this DataSource. This will
        // add some overhead, but will work in many cases.
        return true;
    }

    protected void issueInitialSql(Connection connection) {
        // A typical JDBC statement execution, adapted to
        // your particular needs
        ...
    }

    // Delegate every other method to this.delegate
    ...
}

您可以對每個新連接(或在最壞的情況下,對連接池中的每個“獲取”)執行自定義代碼。 另一方面,如果可行,可以在特定模式上創建LOGON TRIGGER。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM