Java.sql.connection对象第二次不起作用

时间:2015-06-26 05:53:01

标签: java jdbc jboss

我已在我的程序中为jboss应用程序服务器声明了Resource注释。我正在使用这些注释创建连接对象。当我第一次使用连接时,它建立连接,并通过使用java.sql.ResultSetMetaData我能够获取值。但是第二次我得到异常,因为“java.sql.SQLException:Connection与托管的connection.org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6@19adb没有关联”。为什么我得到这个例外。

我正在给我的jboss standalone.xml文件和我的java程序,如下所示,

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>

我的java程序:

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

我正在创建hashmap并将所有连接对象放在该hashmap中,并使用该hashmap来获取连接对象。但是第一次没有工作。

帮助我。

1 个答案:

答案 0 :(得分:4)

您在parseColumnnames结束时关闭了连接 - 所以当您稍后尝试使用它时,它无法正常工作......您无法使用关闭连接。

我建议你不要尝试缓存这样的连接。相反,依靠数据源实现来适当地池连接,并且每次想要连接时只需调用getConnection()。 (我还建议使用try-with-resources来关闭事物,而不是手动完成所有操作。)

那样:

  • 您将超越当前的问题
  • 您不必担心处理尝试同时使用同一连接的多个线程
  • 您在 代码中拥有明确的连接生命周期,并且&#34;我们如何使其高效?#34;连接池,其唯一的原因是为了完成这项工作