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

发布于 2024-12-22 06:24:33 字数 1718 浏览 5 评论 0原文

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

它在我的 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>

我在 servlet 代码中像这样绑定到我的数据源:

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

我的问题是:

我需要 4 个 servlet在同一数据源上使用此上下文查找。这样的事有可能吗?

我的意思是,多个 servlet 是否可以绑定到同一个数据源,或者每个 servlet 必须有自己的数据源吗?

我问这个问题是因为我有一个 servlet 工作正常,但另一个 servlet 抛出 javax.naming.NameNotFoundException (剩余名称 jdbc/MySQL_DS)。

谢谢!

I have a datasource set in my Jetty.xml file that looks like this:

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

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

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. Is such a thing even possible?

I mean, can multiple servlets bind to the same datasource, or must each servlet have their own one?

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

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言月老 2024-12-29 06:24:33

据我所知,您不必为每个 servlet 指定唯一的数据源。

我正在开发一个 Java EE Web 应用程序,其中包含多个 servlet,所有这些 servlet 都使用一个数据源来连接到数据库。该Web应用程序使用Oracle作为后端,WebLogic Server作为应用程序服务器。

在此 Web 应用程序的体系结构中,有一个用于连接数据库的专用类。所有 servlet 都调用此类来获取与数据库的连接。

该连接类在构造函数中具有以下几行(与上面的类似)...

InitialContext ic=new InitialContext();
DataSource ds=(DataSource) ic.lookup("jdbc/OracleDS");
con=ds.getConnection("用户","密码"); \\(“con”是一个私有 Connection 实例变量)

然后,每个 servlet 仅使用连接类来连接到数据库。

例如...

MyConnectionClass con = new MyConnectionClass(); //(“MyConnectionClass”是数据源信息所在的位置...)
PreparedStatement ps=con.prepareStatement("SELECT * FROM SOME_TABLE");
ResultSet rs=ps.executeQuery();
下面有更多代码...

As far as I know, you shouldn't have to specify unique data sources for each 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. This web application uses Oracle as the back-end and WebLogic Server as the application server.

In this web application's architecture, there's a dedicated class for connecting to the DB. All of the servlets invoke this class to get a connection to the 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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文