Java 多个servlet可以绑定到同一个数据源(JNDI)吗?
我在Jetty.xml文件中设置了如下数据源:Java 多个servlet可以绑定到同一个数据源(JNDI)吗?,java,jetty,jndi,c3p0,Java,Jetty,Jndi,C3p0,我在Jetty.xml文件中设置了如下数据源: <New id="MySQL_DS" class="org.eclipse.jetty.plus.jndi.Resource"> <Arg></Arg> <Arg>jdbc/MySQL_DS</Arg> <Arg> <New class="com.mchange.v2.c3p0.ComboPooledDataSource"> <
<New id="MySQL_DS" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/MySQL_DS</Arg>
<Arg>
<New class="com.mchange.v2.c3p0.ComboPooledDataSource">
<Set name="driverClass">com.mysql.jdbc.Driver</Set>
<Set name="jdbcUrl">jdbc:mysql:[IP]</Set>
<Set name="user">[USER]</Set>
<Set name="password">[PASSWORD]</Set>
<Set name="checkoutTimeout">5000</Set>
<Set name="initialPoolSize">3</Set>
<Set name="maxIdleTime">3600</Set>
<Set name="maxPoolSize">50</Set>
<Set name="minPoolSize">1</Set>
<Set name="maxStatements">200</Set>
<Set name="maxConnectionAge">0</Set>
<Set name="acquireIncrement">3</Set>
</New>
</Arg>
</New>
我的问题是:
我需要在同一数据源上有4个servlet使用此上下文查找。这样的事情可能吗
我的意思是,多个servlet可以绑定到同一个数据源,还是每个servlet都必须有自己的数据源
我这样问是因为我有一个servlet工作正常,但另一个servlet抛出javax.naming.NameNotFoundException(剩余名称为jdbc/MySQL\DS)
谢谢 据我所知,您不应该为每个servlet指定唯一的数据源 我正在开发一个JavaEEWeb应用程序,其中有多个servlet,它们都使用一个数据源连接到数据库。此web应用程序使用Oracle作为后端,使用WebLogic服务器作为应用程序服务器 在这个web应用程序的体系结构中,有一个专用类用于连接数据库。所有servlet都调用该类以获得到数据库的连接 此连接类在构造函数中有以下行(与上面的类似)
InitialContext ic=new InitialContext()代码>
datasourceds=(DataSource)ic.lookup(“jdbc/OracleDS”)代码>
con=ds.getConnection(“用户”、“pwd”);\ \(“con”是一个私有连接实例var)
然后,每个servlet只使用connection类连接到DB中
例如
MyConnectionClass con=新的MyConnectionClass();//(“MyConnectionClass”是数据源信息所在的位置…
PreparedStatement ps=con.preparest陈述(“从某些表格中选择*)代码>
ResultSet rs=ps.executeQuery()代码>
下面有更多代码…
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/MySQL_DS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
InitialContext ctx = new InitialContext();
_dataSource = (DataSource)ctx.lookup("java:comp/env/jdbc/MySQL_DS");