JBoss 数据库连接池 [英] JBoss Database Connection Pool

查看:24
本文介绍了JBoss 数据库连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

谁能给我指点一下在网络应用程序中使用 jboss 数据源的教程或代码示例.

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

推荐答案

JBoss 中的池都是在 DataSource 配置中处理的.这里是操作方法.Web 应用程序必须对数据源执行 JNDI 查找以获取数据库连接,而不是执行直接的 JDBC 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.

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

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();

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

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.

以上不包括资源利用率和错误处理问题.您应该在完成后关闭该 Context 对象,当然还有数据库连接,尽管如果您忘记关闭数据库连接并且事务结束,JBoss 会冲您大喊大叫,并为您关闭它.

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.

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

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

这篇关于JBoss 数据库连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆