gpt4 book ai didi

java - 在运行时更改 WebSphere 数据源架构

转载 作者:行者123 更新时间:2023-11-30 06:30:42 25 4
gpt4 key购买 nike

是否可以在运行时更改 WebSphere 数据源的默认架构?

例如,我将以下 jndi 配置为:

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/test" lookup-on-startup="true" resource-ref="true" />

在我的 ibm-web-bnd.xml 中:

<resource-ref name="jdbc/test" binding-name="jdbc/test" id="TEST_db"/>

在我的 ibm-web-ext.xml 中:

<resource-ref name="jdbc/test" connection-management-policy="DEFAULT" isolation-level="TRANSACTION_READ_COMMITTED" id="TEST_db"/>

我为数据源创建了一个包装器,bean 定义为:

<bean id="schemaAwareDataSource" class="com.util.SchemaAwareDataSource" >
<property name="dataSource" ref="dataSource" />
<property name="schemaValue" value="${modifiableSchema}" />
</bean>

这个类看起来像:

public class SchemaAwareDataSource implements DataSource {

private DataSource dataSource;
private String schemaValue;

@Override
public Connection getConnection() throws SQLException {

if(dataSource.getConnection().getSchema().equals(schemaValue)) {
return dataSource.getConnection();
}

dataSource.getConnection().setSchema(schemaValue);
return dataSource.getConnection();
}

//other implemented methods not shown
}

但是,当应用程序启动并调用 dataSource.getConnection().getSchema() 时,这会引发 java.sql.SQLFeatureNotSupportedException 错误 在我的包装类的 getConnection() 中。

当我调试 getConnection() 方法时,dataSource 是一个 com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource 对象。

无论如何,我可以完成我在这里想做的事情吗?我希望能够在运行时调用 getConnection() 来更改架构值

最佳答案

当 JDBC 驱动程序或 WebSphere Application Server 级别(或两者)不支持 JDBC 4.1 时,将引发 SQLFeatureNotSupportedException。如果使用 WebSphere Application Server Liberty,您将需要启用 jdbc-4.1 或 jdbc-4.2 功能部件而不是 jdbc-4.0。如果使用传统的 WebSphere Application Server,您需要确保您使用的是足够新的版本来支持 JDBC 4.1(或更高版本)。无论哪种情况,请通过(在应用程序服务器外部)调用connection.getMetaData().getJDBCMajorVersion 和connection.getMetaData().getJDBCMinorVersion 来检查您的 JDBC 驱动程序是否符合 JDBC 4.1 或更高版本。

关于java - 在运行时更改 WebSphere 数据源架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46204110/

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