JBoss数据库连接池

发布于 2024-07-18 02:10:05 字数 196 浏览 6 评论 0原文

我是 jboss 的新手,我被要求将 jboss 连接池机制与现有的 Web 应用程序合并。 考虑到 Web 应用程序数据库层已正确编写,即所有结果集、语句和连接在不需要时正确关闭,在正确配置 jboss 数据源后,我必须在 Web 应用程序中进行哪些所有代码更改。

任何人都可以向我指出在 Web 应用程序中使用 jboss 数据源的教程或代码示例吗?

I am new to jboss and i have been asked to incorporate jboss connection pooling mechanism with an existing web application. Considering that a web application database layer is properly written i.e. all resultsets, statements and connections being closed properly when not needed, What all code changes i will have to make in my web app after i have configured the jboss datasource properly.

Can anybody please point me to a tutorial or a code sample which uses jboss datasource in a web app.

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

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

发布评论

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

评论(3

失去的东西太少 2024-07-25 02:10:05

首先创建一个名为 xxx-ds.xml 的 xml 文件,并将该文件放入 server/default/deploy/xxx-ds.xml

<datasources>
<local-tx-datasource>
<jndi-name>/jdbc/Exp</jndi-name>
<type-mapping>SQL</type-mapping>
<connection-url>jdbc:microsoft:sqlserver://          </connection-url>
<driver-class>com.microsoft.jdbc.sqlserver.SQLServerDriver</driver-class>
<user-name></user-name>
<password></password>
<min-pool-size>5</min-pool-size>
<max-pool-size>1000</max-pool-size>
</local-tx-datasource>
</datasources>

< strong>jboss-web.xml

<jboss-web>
<!--  <security-domain flushOnSessionInvalidation="false"/>-->
<!--  <context-root>/BSI</context-root>-->
  <resource-ref>
        <description>Database connection resource</description>
        <res-ref-name>jdbc/Exp</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <jndi-name>java:/jdbc/Exp</jndi-name>
        <res-auth>Container</res-auth>
    </resource-ref>
</jboss-web>

web.xml

<resource-ref>
    <description>Database connection resource</description>   
    <res-ref-name>jdbc/Exp</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

现在位于您的 .java 文件中

javax.naming.Context ctx1 = new javax.naming.InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx1.lookup("java:comp/env/jdbc/Exp");
con = ds.getConnection();

* **** 确保资源引用名称在所有地方都应该相同

first create an xml file by name xxx-ds.xml and place this file in server/default/deploy/xxx-ds.xml

<datasources>
<local-tx-datasource>
<jndi-name>/jdbc/Exp</jndi-name>
<type-mapping>SQL</type-mapping>
<connection-url>jdbc:microsoft:sqlserver://          </connection-url>
<driver-class>com.microsoft.jdbc.sqlserver.SQLServerDriver</driver-class>
<user-name></user-name>
<password></password>
<min-pool-size>5</min-pool-size>
<max-pool-size>1000</max-pool-size>
</local-tx-datasource>
</datasources>

jboss-web.xml

<jboss-web>
<!--  <security-domain flushOnSessionInvalidation="false"/>-->
<!--  <context-root>/BSI</context-root>-->
  <resource-ref>
        <description>Database connection resource</description>
        <res-ref-name>jdbc/Exp</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <jndi-name>java:/jdbc/Exp</jndi-name>
        <res-auth>Container</res-auth>
    </resource-ref>
</jboss-web>

web.xml

<resource-ref>
    <description>Database connection resource</description>   
    <res-ref-name>jdbc/Exp</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

and now in your .java file

javax.naming.Context ctx1 = new javax.naming.InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx1.lookup("java:comp/env/jdbc/Exp");
con = ds.getConnection();

***** make sure that resource ref name should be same in all place

念﹏祤嫣 2024-07-25 02:10:05

JBoss 中的池全部在 DataSource 配置中处理。 这里是操作方法。 Web 应用程序必须对数据源进行 JNDI 查找才能获取数据库连接,而不是直接执行 JDBC URL,然后您将拥有池化功能。

不过,交易则是另一回事了。

编辑:为了回应您关于这如何影响代码的评论,这就是它的样子:

String jndiPath = "java:DataSourceJNDIName"; //The exact prefix here has a lot to do with clustering, etc., but if you are using one JBoss instance standalone, this works.
Context ctx = new InitialContext();
DataSource ds = (DataSource) PortableRemoteObject.narrow(ctx.lookup(jndiPath), DataSource.class);
Connection c = ds.getConnection();

从技术上讲,PortableRemoteObject.narrow 在 JBoss (无论如何 4.2.2)单服务器配置中肯定是不必要的,但它更重要正确的 J2EE 标准代码,因为一般应用程序服务器不必仅为执行 Context.lookup 而返回正确类型的对象。

以上并未涵盖资源利用和错误处理问题。 当你使用完 Context 对象后,你应该关闭它,当然还有数据库连接,尽管如果你忘记关闭数据库连接并且事务结束,JBoss 会对你大喊大叫,并为你关闭它。

无论如何,该 Connection 对象与 DriverManager.getConnection(url); 一样可用。

The pool in JBoss is all handled in the DataSource configuration. Here is the HowTo. The web app would have to do a JNDI lookup for the datasource to get the database connection rather than doing a direct JDBC URL, and then you will have pooling.

Transactions are another story, though.

EDIT: In response to your comment about how this affects the code, this is what it looks like:

String jndiPath = "java:DataSourceJNDIName"; //The exact prefix here has a lot to do with clustering, etc., but if you are using one JBoss instance standalone, this works.
Context ctx = new InitialContext();
DataSource ds = (DataSource) PortableRemoteObject.narrow(ctx.lookup(jndiPath), DataSource.class);
Connection c = ds.getConnection();

Technically speaking the PortableRemoteObject.narrow isn't necessary in a JBoss (4.2.2 anyway) single server configuration for sure, but it is more proper J2EE standard code, as general application servers don't have to return an object of the right type just for doing a Context.lookup.

The above doesn't cover the resource utilization and error handling issues. You are supposed to close that Context object when you are done with it, and of course the database connection, although JBoss will yell at you if you forget to close the database connection and the transaction ends, and close it for you.

Anyway, that Connection object is usable just as much as DriverManager.getConnection(url);

天邊彩虹 2024-07-25 02:10:05

你不需要改变任何东西。
当您选择正确类型的数据源(local-tx-datasource / xa-datasource)时,连接处理和 TX 就会为您完成。 在 $JBoss/docs/examples/jca 中,您将找到几乎每个数据库的模板,您可以重复使用它们。

如果您使用XA,则需要配置Tx-recovery。 请参阅此帖子以了解操作方法:
http://management-platform.blogspot.com/ 2008/11/transaction-recovery-in-jbossas.html(好吧,也许不是独立模式下的操作方法,而是与 Jopr 源代码结合使用)。

You don't have to change anything.
When you select the right kind of data source (local-tx-datasource / xa-datasource), connection handling and TX is done for you. In $JBoss/docs/examples/jca you will find templates for virtually every database, that you can just reuse.

If you are using XA, you need to configure Tx-recovery. See this posting on a how-to:
http://management-platform.blogspot.com/2008/11/transaction-recovery-in-jbossas.html (well, perhaps not a how-to in the standalone mode, but in conjunction with the Jopr source code).

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