2013-03-18 60 views

回答

0

您可以在每個新連接上執行自定義代碼(或者,在最壞的情況下,每個連接池中的「get」)。另一方面,如果可行,您可以在特定模式上創建登錄觸發器。

+0

所以問題是我怎麼做每個新的連接自定義代碼?對每個「獲取」做任何事情都會產生太多的影響。出於多種原因,執行登錄觸發器不是一個選項。 – 2013-03-18 22:17:26

+0

@MK。哪一個更改會話如此昂貴以至於每次交易都無法承擔額外的一次通話費用? – gpeche 2013-05-10 22:33:41

+0

@gpeche我不記得了,但通常我不想找到我們艱難的方式哪些是昂貴的,寧願做正確的事情沒有花費幾個小時後分析性能。 – 2013-05-11 15:54:39

1

如果您正在部署到應用程序服務器,請檢查配置選項,在很多情況下,您可以在其中配置一些「初始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 
    ... 
} 
相關問題