JBOSS 应用程序服务器下的 Java 应用程序 运行 中的数据库连接池
Database Connection pooling in a Java application running under JBOSS application server
我正在尝试了解部署在 JBOSS 下的 java 应用程序中的数据库连接池。顺便说一句,数据库连接没有问题。
在 JBOSS 应用程序服务器中设置了一个数据库连接池,如下所示:
<datasource jta="false" jndi-name="java:/testDS" pool-name="testDS" enabled="true" use-java-context="true">
<connection-url>jdbc:oracle:thin:@xxx</connection-url>
<driver>oracle</driver>
<pool>
<min-pool-size>2</min-pool-size>
<max-pool-size>15</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<user-name>admin</user-name>
<password>admin</password>
</security>
</datasource>
然后我的 java 获取连接的代码如下所示:
String jndiName = "java:/testDS";
InitialContext jndiCntx = new InitialContext();
DataSource ds = (DataSource) jndiCntx.lookup(jndiName);
Connection connection = ds.getConnection();
以上代码是否使用了连接池?如果是,下面代码的目的是什么?我有点困惑。这 2 个代码片段有什么区别?
InitialContext jndiCntx = new InitialContext();
ConnectionPoolDataSource cpds = (ConnectionPoolDataSource) jndiCntx.lookup(jndiName);
PooledConnection pc = cpds.getPooledConnection();
Connection connection = pc.getConnection();
如果您查看 PooledConnection 中的 JavaDoc (
http://docs.oracle.com/javase/7/docs/api/javax/sql/PooledConnection.html) 由 ConnectionPoolDataSource 返回,您可以阅读:
An application programmer does not use the PooledConnection interface directly; rather, it is used by a middle tier infrastructure that manages the pooling of connections.
When an application calls the method DataSource.getConnection, it gets
back a Connection object. If connection pooling is being done, that
Connection object is actually a handle to a PooledConnection object,
which is a physical connection.
数据源的典型用法如下所示:
@Stateless
public class MyBean {
@Resource(lookup = "java:/testDS")
private DataSource dataSource;
public void testDatasource() {
try (Connection connection = dataSource.getConnection()) {
// use the connection
} catch (SQLException e) {
throw new SomeRuntimeException(e);
}
}
}
重要的是您在使用后关闭连接。最好的方法是尝试自动关闭。否则您的服务器会耗尽连接。
使用“@Statless”发起交易。
我正在尝试了解部署在 JBOSS 下的 java 应用程序中的数据库连接池。顺便说一句,数据库连接没有问题。 在 JBOSS 应用程序服务器中设置了一个数据库连接池,如下所示:
<datasource jta="false" jndi-name="java:/testDS" pool-name="testDS" enabled="true" use-java-context="true">
<connection-url>jdbc:oracle:thin:@xxx</connection-url>
<driver>oracle</driver>
<pool>
<min-pool-size>2</min-pool-size>
<max-pool-size>15</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<user-name>admin</user-name>
<password>admin</password>
</security>
</datasource>
然后我的 java 获取连接的代码如下所示:
String jndiName = "java:/testDS";
InitialContext jndiCntx = new InitialContext();
DataSource ds = (DataSource) jndiCntx.lookup(jndiName);
Connection connection = ds.getConnection();
以上代码是否使用了连接池?如果是,下面代码的目的是什么?我有点困惑。这 2 个代码片段有什么区别?
InitialContext jndiCntx = new InitialContext();
ConnectionPoolDataSource cpds = (ConnectionPoolDataSource) jndiCntx.lookup(jndiName);
PooledConnection pc = cpds.getPooledConnection();
Connection connection = pc.getConnection();
如果您查看 PooledConnection 中的 JavaDoc ( http://docs.oracle.com/javase/7/docs/api/javax/sql/PooledConnection.html) 由 ConnectionPoolDataSource 返回,您可以阅读:
An application programmer does not use the PooledConnection interface directly; rather, it is used by a middle tier infrastructure that manages the pooling of connections.
When an application calls the method DataSource.getConnection, it gets back a Connection object. If connection pooling is being done, that Connection object is actually a handle to a PooledConnection object, which is a physical connection.
数据源的典型用法如下所示:
@Stateless
public class MyBean {
@Resource(lookup = "java:/testDS")
private DataSource dataSource;
public void testDatasource() {
try (Connection connection = dataSource.getConnection()) {
// use the connection
} catch (SQLException e) {
throw new SomeRuntimeException(e);
}
}
}
重要的是您在使用后关闭连接。最好的方法是尝试自动关闭。否则您的服务器会耗尽连接。
使用“@Statless”发起交易。