I have declared Resource annotation in my program for the jboss application server. I am creating connection objects using these annotations. When i am using connection for first time it establishes connection and by using java.sql.ResultSetMetaData i am able to get values. But on second time i am getting exception as "java.sql.SQLException: Connection is not associated with a managed connection.org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6@19adb". Why I am getting this exception.
I am giving my jboss standalone.xml file and my java program as follows,
standalone.xml:
<datasources>
<datasource jndi-name="java:jboss/datasources/dcimpl" pool-name="dcimpl_pool" enabled="true" jta="true" use-java-context="true" use-ccm="true">
<connection-url>
jdbc:mysql://localhost:3306/dcimpl?zeroDateTimeBehavior=convertToNull
</connection-url>
<driver>
mysqldriver
</driver>
<transaction-isolation>
TRANSACTION_READ_COMMITTED
</transaction-isolation>
<pool>
<min-pool-size>
10
</min-pool-size>
<max-pool-size>
50
</max-pool-size>
<prefill>
true
</prefill>
<use-strict-min>
false
</use-strict-min>
<flush-strategy>
FailingConnectionOnly
</flush-strategy>
</pool>
<security>
<user-name>
root
</user-name>
<password>
</password>
</security>
<validation>
<validate-on-match>
false
</validate-on-match>
<background-validation>
false
</background-validation>
<useFastFail>
false
</useFastFail>
</validation>
<statement>
<prepared-statement-cache-size>
100
</prepared-statement-cache-size>
<share-prepared-statements/>
</statement>
</datasource>
<datasource jndi-name="java:jboss/datasources/dcframework" pool-name="dcframework_pool" enabled="true" jta="true" use-java-context="true" use-ccm="true">
<connection-url>
jdbc:mysql://localhost:3306/dcframework?zeroDateTimeBehavior=convertToNull
</connection-url>
<driver>
mysqldriver
</driver>
<transaction-isolation>
TRANSACTION_READ_COMMITTED
</transaction-isolation>
<pool>
<min-pool-size>
10
</min-pool-size>
<max-pool-size>
50
</max-pool-size>
<prefill>
true
</prefill>
<use-strict-min>
false
</use-strict-min>
<flush-strategy>
FailingConnectionOnly
</flush-strategy>
</pool>
<security>
<user-name>
root
</user-name>
<password>
</password>
</security>
<validation>
<validate-on-match>
false
</validate-on-match>
<background-validation>
false
</background-validation>
<useFastFail>
false
</useFastFail>
</validation>
<statement>
<prepared-statement-cache-size>
100
</prepared-statement-cache-size>
<share-prepared-statements/>
</statement>
</datasource>
<datasource jndi-name="java:jboss/datasources/dctest1" pool-name="dcframework_pool" enabled="true" jta="true" use-java-context="true" use-ccm="true">
<connection-url>
jdbc:mysql://localhost:3306/dc_testing1?zeroDateTimeBehavior=convertToNull
</connection-url>
<driver>
mysqldriver
</driver>
<transaction-isolation>
TRANSACTION_READ_COMMITTED
</transaction-isolation>
<pool>
<min-pool-size>
10
</min-pool-size>
<max-pool-size>
50
</max-pool-size>
<prefill>
true
</prefill>
<use-strict-min>
false
</use-strict-min>
<flush-strategy>
FailingConnectionOnly
</flush-strategy>
</pool>
<security>
<user-name>
root
</user-name>
<password>
</password>
</security>
<validation>
<validate-on-match>
false
</validate-on-match>
<background-validation>
false
</background-validation>
<useFastFail>
false
</useFastFail>
</validation>
<statement>
<prepared-statement-cache-size>
100
</prepared-statement-cache-size>
<share-prepared-statements/>
</statement>
</datasource>
<datasource jndi-name="java:jboss/datasources/dctest2" pool-name="dcframework_pool" enabled="true" jta="true" use-java-context="true" use-ccm="true">
<connection-url>
jdbc:mysql://localhost:3306/dc_testing2?zeroDateTimeBehavior=convertToNull
</connection-url>
<driver>
mysqldriver
</driver>
<transaction-isolation>
TRANSACTION_READ_COMMITTED
</transaction-isolation>
<pool>
<min-pool-size>
10
</min-pool-size>
<max-pool-size>
50
</max-pool-size>
<prefill>
true
</prefill>
<use-strict-min>
false
</use-strict-min>
<flush-strategy>
FailingConnectionOnly
</flush-strategy>
</pool>
<security>
<user-name>
root
</user-name>
<password>
</password>
</security>
<validation>
<validate-on-match>
false
</validate-on-match>
<background-validation>
false
</background-validation>
<useFastFail>
false
</useFastFail>
</validation>
<statement>
<prepared-statement-cache-size>
100
</prepared-statement-cache-size>
<share-prepared-statements/>
</statement>
</datasource>
<datasource jndi-name="java:jboss/datasources/dctest3" pool-name="dcframework_pool" enabled="true" jta="true" use-java-context="true" use-ccm="true">
<connection-url>
jdbc:mysql://localhost:3306/dc_testing3?zeroDateTimeBehavior=convertToNull
</connection-url>
<driver>
mysqldriver
</driver>
<transaction-isolation>
TRANSACTION_READ_COMMITTED
</transaction-isolation>
<pool>
<min-pool-size>
10
</min-pool-size>
<max-pool-size>
50
</max-pool-size>
<prefill>
true
</prefill>
<use-strict-min>
false
</use-strict-min>
<flush-strategy>
FailingConnectionOnly
</flush-strategy>
</pool>
<security>
<user-name>
root
</user-name>
<password>
</password>
</security>
<validation>
<validate-on-match>
false
</validate-on-match>
<background-validation>
false
</background-validation>
<useFastFail>
false
</useFastFail>
</validation>
<statement>
<prepared-statement-cache-size>
100
</prepared-statement-cache-size>
<share-prepared-statements/>
</statement>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>
org.h2.jdbcx.JdbcDataSource
</xa-datasource-class>
</driver>
<driver name="postgresql" module="org.postgresql">
<xa-datasource-class>
org.postgresql.xa.PGXADataSource
</xa-datasource-class>
</driver>
<driver name="mysqldriver" module="com.mysql"/>
</drivers>
</datasources>
My java program :
@Stateless
public class DispalyConfigScreenBO {
HashMap<String, Connection> memoryConn = new HashMap<String, Connection>();
@Resource(mappedName = "java:jboss/datasources/dctest1")
DataSource dataSource1;
@Resource(mappedName = "java:jboss/datasources/dctest2")
DataSource dataSource2;
@Resource(mappedName = "java:jboss/datasources/dctest3")
DataSource dataSource3;
public String createPoolConns() {
try {
Connection con1 = dataSource1.getConnection();
memoryConn.put("dctest1", con1);
Connection con2 = dataSource2.getConnection();
memoryConn.put("dctest2", con2);
Connection con3 = dataSource3.getConnection();
memoryConn.put("dctest3", con3);
} catch (Exception e) {
System.out.println("Exception in createPoolConns = " + e);
}
return "success";
}
public String parseColumnnames(String value, String datasource)
throws Exception {
StringBuilder output = new StringBuilder();
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = memoryConn.get(datasource);
if (conn == null) {
return null;
} else {
st = conn.createStatement();
rs = st.executeQuery((value + " LIMIT 1").replace(";", ""));
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
for (int i = 1; i <= numberOfColumns; i++) {
if (i == numberOfColumns) {
output.append(rsMetaData.getColumnLabel(i));
} else {
output.append(rsMetaData.getColumnLabel(i));
output.append(",");
}
}
}
} catch (Exception dbException) {
dbException.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
}
return output.toString();
}
I am creating hashmap and put all the connection objects in that hashmap and using that hashmap where ever neccessary to get connection objects. But is not working except for first time.
Help me.
You're closing the connection at the end of parseColumnnames
- so when you try to use it later on, it won't work... you can't use a closed connection.
I would suggest you don't try to cache connections like this. Instead, rely on the data source implementation to pool connections appropriately, and just call getConnection()
each time you want a connection. (I'd also advise using try-with-resources to close things instead of doing it all manually.)
That way:
See more on this question at Stackoverflow