gpt4 book ai didi

java - oracle.jdbc.pool.OracleDataSource 为每个新连接运行一个命令

转载 作者:太空宇宙 更新时间:2023-11-04 07:44:47 25 4
gpt4 key购买 nike

我想对来自特定 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
...
}

关于java - oracle.jdbc.pool.OracleDataSource 为每个新连接运行一个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15487300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com