简体   繁体   English

多个servlet可以绑定到同一个数据源(JNDI)吗?

[英]Can multiple servlets bind to the same datasource (JNDI)?

I have a datasource set in my Jetty.xml file that looks like this: 我在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">
      <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>

It is defined in my web.xml like so: 它在我的web.xml中定义如下:

<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>

And I bind to my datasource like this in my servlet code: 我在我的servlet代码中绑定到我的数据源:

InitialContext ctx = new InitialContext();
_dataSource = (DataSource)ctx.lookup("java:comp/env/jdbc/MySQL_DS");

My question is: 我的问题是:

I need to have 4 servlet using this context lookup, on the same datasource. 我需要在同一个数据源上使用这个上下文查找有4个servlet。 Is such a thing even possible? 这样的事情甚至可能吗?

I mean, can multiple servlets bind to the same datasource, or must each servlet have their own one? 我的意思是,多个servlet可以绑定到同一个数据源,还是每个servlet都有自己的一个?

I'm asking this because I have one servlet that is working correctly but another one throws a javax.naming.NameNotFoundException (remaining name jdbc/MySQL_DS). 我问这个是因为我有一个正常工作的servlet但另一个抛出了一个javax.naming.NameNotFoundException(剩余名称为jdbc / MySQL_DS)。

Thanks! 谢谢!

As far as I know, you shouldn't have to specify unique data sources for each servlet. 据我所知,您不必为每个servlet指定唯一的数据源。

I'm working on a Java EE web application that has multiple servlets in it that all use one data source for connecting to the DB. 我正在开发一个Java EE Web应用程序,其中包含多个servlet,它们都使用一个数据源连接到DB。 This web application uses Oracle as the back-end and WebLogic Server as the application server. 此Web应用程序使用Oracle作为后端,使用WebLogic Server作为应用程序服务器。

In this web application's architecture, there's a dedicated class for connecting to the DB. 在此Web应用程序的体系结构中,有一个用于连接到DB的专用类。 All of the servlets invoke this class to get a connection to the DB. 所有servlet都会调用此类来获取与DB的连接。

This connection class has the following lines in the constructor (similar to yours above)... 此连接类在构造函数中具有以下行(与您的类似)...

InitialContext ic=new InitialContext();
DataSource ds=(DataSource) ic.lookup("jdbc/OracleDS");
con=ds.getConnection("user","pwd"); \\\\ ("con" is a private Connection instance var)

Then, each of the servlets just use the connection class to connect into the DB. 然后,每个servlet只使用连接类连接到DB。

For instance... 例如...

MyConnectionClass con = new MyConnectionClass(); // ("MyConnectionClass" is where the data source info is...)
PreparedStatement ps=con.prepareStatement("SELECT * FROM SOME_TABLE");
ResultSet rs=ps.executeQuery();
more code below...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM