1

I would like to execute an ALTER SESSION command on every new connection coming from a particular oracle.jdbc.pool.OracleDataSource connection pool. Is there a way to do that?

4

2 回答 2

1

如果要部署到应用服务器,请检查配置选项,在许多情况下,您可以在那里配置一些“初始 SQL”。

如果您不能这样做,请oracle.jdbc.pool.OracleDataSource在应用程序级别使用自定义 DataSource 进行包装,然后在应用程序的其余部分使用该自定义 DataSource。就像是:

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
    ...
}
于 2013-05-10T08:35:05.757 回答
0

您可以在每个新连接上执行自定义代码(或者,在最坏的情况下,在连接池的每个“get”上)。另一方面,如果可行,您可以在特定模式上创建 LOGON TRIGGER。

于 2013-03-18T22:10:24.793 回答