指定默认模式 WebSpehere 8.5 服务器

Specifying default schema WebSpehere 8.5 Server

我正在进行一个迁移项目,我们正在将一个应用程序从 Weblogic 迁移到 Websphere 8.5 服务器。

在 Weblogic 服务器中,我们可以在创建数据源时指定默认架构,但我在 WebSpehere 8.5 服务器中看不到相同的选项。

有没有自定义的属性我们可以设置它,我试过currentSchema=MySchema但是没有用。

据我所知,Weblogic 只允许通过设置“Init SQLto a SQL string which sets the current schema in the database, such asSQL ALTER SESSION SET CURRENT_SCHEMA=MySchema”来设置默认架构。因此,这个答案假设设置数据源当前模式的唯一方法是通过 SQL.

在 WebSphere 中,最接近 WebLogic 的 Init SQL 的是 WebSphere 上的 preTestSQLString 属性。

preTestSQLString 属性 的想法是 WebSphere 将执行一个非常简单的 SQL 语句来验证您是否可以在服务器启动时正确连接到数据库。通常这个 属性 的值是非常基本的东西,比如 select 1 from dual', but since you can put in whatever SQL you want, you could setpreTestSQLStringtoSQL ALTER SESSION SET CURRENT_SCHEMA=MySchema`.

来自 WebSphere 文档的步骤 (link):

  1. In the administrative console, click Resources > JDBC providers.
  2. Select a provider and click Data Sources under Additional properties.
  3. Select a data source and click WebSphere Application Server data source properties under Additional properties.
  4. Select the PreTest Connections check box.
  5. Type a value for the PreTest Connection Retry Interval, which is measured in seconds. This property determines the frequency with which a new connection request is made after a pretest operation fails.
  6. Type a valid SQL statement for the PreTest SQL String. Use a reliable SQL command, with minimal performance impact; this statement is processed each time a connection is obtained from the free pool. For example, "select 1 from dual" in oracle or "SQL select 1" in SQL Server.

这个答案需要做更多的工作,但我将其包括在内是因为它是设计的解决方案,可以自定义连接的几乎所有内容,包括架构。 WebSphere Application Sever 允许您 provide/extend DataStoreHelper。

Knowledge Center document on providing a custom DataStoreHelper

在这种情况下,您可以扩展 com.ibm.websphere.rsadapter.Oracle11gDataStoreHelper

JavaDoc for Oracle11gDataStoreHelper

将对以下方法感兴趣:

  • doConnectionSetup,在第一次创建连接时对连接进行一次性初始化
  • doConnectionCleanup,在将连接返回到连接池之前重置连接状态。

当您重写 doConnectionSetup 时,您将获得新创建的连接,您可以在此基础上执行操作,

super.doConnectionSetup(connection);
Statement stmt = connection.createStatement();
try {
    stmt.execute(sqlToUpdateSchema);
} finally {
    stmt.close();
}

doConnectionCleanup 让您可以考虑使用连接的应用程序代码可能将架构切换为其他内容的可能性。 doConnectionCleanup 让您有机会重置它。同样,您将获得一个连接,您可以在该连接上执行操作,

super.doConnectionCleanup(connection);
Statement stmt = connection.createStatement();
try {
    stmt.execute(sqlToUpdateSchema);
} finally {
    stmt.close();
}

请注意,在这两种情况下,调用相应的超级 class 方法对于确保您不会清除 WebSphere Application Server 内置的特定于数据库的 initialization/cleanup 代码很重要数据库。

通用连接池 (UCP) 是一个 Java 连接池,白皮书“UCP with Webshere" 展示了如何将 UCP 设置为数据源。

对于 JDBC 数据源,步骤类似,但您可以选择默认的 JDBC 驱动程序选项。

查看论文以供参考。