I am trying to understand database connection pool in a java application which is deployed under JBOSS. Btw there is no issue with database connection. A database connection pool is setup in JBOSS application server like below:
<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>
Then my java code to get the connection looks like below:
String jndiName = "java:/testDS";
InitialContext jndiCntx = new InitialContext();
DataSource ds = (DataSource) jndiCntx.lookup(jndiName);
Connection connection = ds.getConnection();
Does the above code make use of connectionpool? If it is, what is the purpose of the below code? I am a bit confused. What is the difference between these 2 code snippets?
InitialContext jndiCntx = new InitialContext();
ConnectionPoolDataSource cpds = (ConnectionPoolDataSource) jndiCntx.lookup(jndiName);
PooledConnection pc = cpds.getPooledConnection();
Connection connection = pc.getConnection();