在 J2EE 中使用多个上下文连接到不同的数据源(Websphere)
Using multiple Contexts in J2EE to connect to different data sources (Websphere)
我有一个 Java EE 应用程序需要连接到两个数据库。一种是内存数据库 (H2),一种是常规 Oracle 数据库。
我可以在 Websphere Application Server 中设置与 Oracle 数据库的连接,在 ibm-web-bnd.xml 中放置一个引用,然后使用以下代码访问它:
DataSource dataSource = null;
try
{
InitialContext ctx = new InitialContext();
// comp/env is for component-environment bindings, such as web application bindings
// this allows us to change the name later in the binding (in the ibm-web-bnd.xml file), instead of here in the code.
dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/refDB");
}
catch (NamingException e)
{
e.printStackTrace();
}
return dataSource.getConnection();
这工作正常 - InitialContext returns 我需要的 WAS 上下文,可以找到数据源,一切都很好。
但是,我还希望能够从同一应用程序访问 H2 数据库。按照我在 pluralsight 上找到的教程,似乎这样做的方法是导入 tomcat 罐子,然后即时创建上下文:
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
InitialContext ctx = new InitialContext();
//"java:comp/env/jdbc/CacheDb" is the context that needs creating here.
ctx.createSubcontext("java:");
ctx.createSubcontext("java:comp");
ctx.createSubcontext("java:comp/env");
ctx.createSubcontext("java:comp/env/jdbc");
JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setURL("jdbc:h2:mem:cachedb;DB_CLOSE_DELAY=-1"); // don't delete the database when the last connection closes
ctx.bind("java:comp/env/jdbc/CacheDb", dataSource);
如果此代码是应用程序中唯一的连接代码,它也可以工作。
如果我的应用程序在某个时候同时调用了两者,我会收到如下错误:
SystemErr R javax.naming.NoInitialContextException: Unable to find
the InitialContextFactory
org.apache.naming.java.javaURLContextFactory.
然后我想,好吧,也许 System.setProperty
代码导致将错误的上下文用于 Oracle 查找 - 所以我尝试在获取 InitialContext 之前使用以下代码专门指定 WAS 上下文Oracle 连接:
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
System.setProperty(Context.URL_PKG_PREFIXES, "com.ibm.websphere.naming");
但是,在配置 H2 数据库上下文时,这给了我同样的错误。
任何人都可以解释我做错了什么,或者为我指出正确的解决方案方向吗?谢谢
我假设您使用的是传统的 WebSphere Application Server,而不是 Liberty。两者都可以像任何其他 JDBC 兼容的数据库一样通过 DataSource 访问 h2 数据库。听起来您已经为 Oracle 配置了 jdbc 提供程序和数据源。您只需按照 IBM KnowledgeCenter topic 中的说明为 h2 创建和配置用户定义的 jdbc 提供程序和关联数据源。由于您在 web.xml 中配置资源引用,让应用服务器为您完成工作,只需将两个数据源注入您的应用程序,如
@Resource(lookup="jdbc/refDB")
DataSource oracleDS;
@Resource(lookup="jdbc/CacheDb")
DataSource h2DS;
您不需要(也不应该)在 JNDI 中进行直接查找,也不必担心 InitialContext。
我有一个 Java EE 应用程序需要连接到两个数据库。一种是内存数据库 (H2),一种是常规 Oracle 数据库。
我可以在 Websphere Application Server 中设置与 Oracle 数据库的连接,在 ibm-web-bnd.xml 中放置一个引用,然后使用以下代码访问它:
DataSource dataSource = null;
try
{
InitialContext ctx = new InitialContext();
// comp/env is for component-environment bindings, such as web application bindings
// this allows us to change the name later in the binding (in the ibm-web-bnd.xml file), instead of here in the code.
dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/refDB");
}
catch (NamingException e)
{
e.printStackTrace();
}
return dataSource.getConnection();
这工作正常 - InitialContext returns 我需要的 WAS 上下文,可以找到数据源,一切都很好。
但是,我还希望能够从同一应用程序访问 H2 数据库。按照我在 pluralsight 上找到的教程,似乎这样做的方法是导入 tomcat 罐子,然后即时创建上下文:
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
InitialContext ctx = new InitialContext();
//"java:comp/env/jdbc/CacheDb" is the context that needs creating here.
ctx.createSubcontext("java:");
ctx.createSubcontext("java:comp");
ctx.createSubcontext("java:comp/env");
ctx.createSubcontext("java:comp/env/jdbc");
JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setURL("jdbc:h2:mem:cachedb;DB_CLOSE_DELAY=-1"); // don't delete the database when the last connection closes
ctx.bind("java:comp/env/jdbc/CacheDb", dataSource);
如果此代码是应用程序中唯一的连接代码,它也可以工作。
如果我的应用程序在某个时候同时调用了两者,我会收到如下错误:
SystemErr R javax.naming.NoInitialContextException: Unable to find the InitialContextFactory org.apache.naming.java.javaURLContextFactory.
然后我想,好吧,也许 System.setProperty
代码导致将错误的上下文用于 Oracle 查找 - 所以我尝试在获取 InitialContext 之前使用以下代码专门指定 WAS 上下文Oracle 连接:
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
System.setProperty(Context.URL_PKG_PREFIXES, "com.ibm.websphere.naming");
但是,在配置 H2 数据库上下文时,这给了我同样的错误。
任何人都可以解释我做错了什么,或者为我指出正确的解决方案方向吗?谢谢
我假设您使用的是传统的 WebSphere Application Server,而不是 Liberty。两者都可以像任何其他 JDBC 兼容的数据库一样通过 DataSource 访问 h2 数据库。听起来您已经为 Oracle 配置了 jdbc 提供程序和数据源。您只需按照 IBM KnowledgeCenter topic 中的说明为 h2 创建和配置用户定义的 jdbc 提供程序和关联数据源。由于您在 web.xml 中配置资源引用,让应用服务器为您完成工作,只需将两个数据源注入您的应用程序,如
@Resource(lookup="jdbc/refDB")
DataSource oracleDS;
@Resource(lookup="jdbc/CacheDb")
DataSource h2DS;
您不需要(也不应该)在 JNDI 中进行直接查找,也不必担心 InitialContext。