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

查看:245
本文介绍了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.

任何人都可以给我指出在Web应用程序中使用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();

从技术上讲,可以肯定,在JBoss(4.2.2)单服务器配置中并不一定需要PortableRemoteObject.narrow,但是它是更合适的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天全站免登陆