2016-02-13 93 views
0

我嘗試用Jmeter強調我的應用程序。當我這樣做不是我的兩個方法同步,我有多個錯誤,如下所示:Jmeter測試的JDCBC連接錯誤

聲明被關閉。 或

22:43:40,669 ERROR [stderr] (http-localhost-127.0.0.1-8080-106) java.sql.SQLException: Error 
22:43:40,669 ERROR [stderr] (http-localhost-127.0.0.1-8080-106)  at org.jboss.jca.adapters.jdbc.WrappedConnection.checkException(WrappedConnection.java:1643) 
22:43:40,670 ERROR [stderr] (http-localhost-127.0.0.1-8080-106)  at org.jboss.jca.adapters.jdbc.WrappedStatement.checkException(WrappedStatement.java:1262) 
22:43:40,670 ERROR [stderr] (http-localhost-127.0.0.1-8080-106)  at org.jboss.jca.adapters.jdbc.WrappedResultSet.checkException(WrappedResultSet.java:4063) 
22:43:40,670 ERROR [stderr] (http-localhost-127.0.0.1-8080-106)  at org.jboss.jca.adapters.jdbc.WrappedResultSet.next(WrappedResultSet.java:1866) 
22:43:40,671 ERROR [stderr] (http-localhost-127.0.0.1-8080-106)  at fr.formation.dao.DaoImpl.getAllPersonnes(DaoImpl.java:275) 

java.sql.SQLException: The result set is closed. 

Caused by: java.lang.NullPointerException 
22:43:40,992 ERROR [stderr] (http-localhost-127.0.0.1-8080-73) at com.mysql.jdbc.ResultSetImpl.checkColumnBounds(ResultSetImpl.java:825) 
22:43:40,993 ERROR [stderr] (http-localhost-127.0.0.1-8080-73) at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2710) 
22:43:40,993 ERROR [stderr] (http-localhost-127.0.0.1-8080-73) at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2846) 
22:43:40,993 ERROR [stderr] (http-localhost-127.0.0.1-8080-73) at org.jboss.jca.adapters.jdbc.WrappedResultSet.getInt(WrappedResultSet.java:1073) 
22:43:40,993 ERROR [stderr] (http-localhost-127.0.0.1-8080-73) ... 23 more 
22:43:40,993 ERROR [stderr] (http-localhost-127.0.0.1-8080-184) java.sql.SQLException: The result set is closed. 
22:43:40,994 ERROR [stderr] (http-localhost-127.0.0.1-8080-184)  at org.jboss.jca.adapters.jdbc.WrappedResultSet.checkState(WrappedResultSet.java:4081) 
22:43:40,994 ERROR [stderr] (http-localhost-127.0.0.1-8080-184)  at org.jboss.jca.adapters.jdbc.WrappedResultSet.getInt(WrappedResultSet.java:1065) 

我的代碼:

public List<PersonneDao> getAllPersonnes() throws SQLException{ 
     List<PersonneDao> liste = new ArrayList<PersonneDao>(); 
      ResultSet rs = null ; 
      PreparedStatement preparedStatement = null; 
      try { 

       connection = ConnectionUtil.getInstance().getConnection(); 
       preparedStatement = (PreparedStatement) connection.prepareStatement("select * from personne"); 
       rs = preparedStatement.executeQuery(); 



       while (rs.next()) { 
       PersonneDao user = new PersonneDao(); 
        user.setId (rs.getInt("id")); 
        user.setNom (rs.getString("nom")); 
        user.setPrenom(rs.getString("prenom")); 
        user.setEmail(rs.getString("email")); 

        liste.add(user); 

       } 

       rs.close(); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      }finally{ 

       if (rs != null) { 
        try { 
         rs.close(); 
        } catch (SQLException sqlex) { 
         // ignore, as we can't do anything about it here 
        } 

        rs = null; 
       } 

       if (preparedStatement != null) { 
        try { 
         preparedStatement.close(); 
        } catch (SQLException sqlex) { 
         // ignore, as we can't do anything about it here 
        } 

        preparedStatement = null; 
       } 

       if (connection != null) { 
        try { 
         ConnectionUtil.getInstance().close(connection); 
        } catch (SQLException sqlex) { 
         // ignore, as we can't do anything about it here 
        } 

        connection = null; 


       } 
      } 
     return liste; 

    } 

} 

public class ConnectionUtil { 

    private DataSource dataSource; 

    private static ConnectionUtil instance = new ConnectionUtil(); 

    private ConnectionUtil() { 
     try { 
      Context initContext = new InitialContext(); 
      dataSource = (DataSource) initContext.lookup("java:/dsFormationJSP"); 
     } catch (NamingException e) { 
      e.printStackTrace(); 
     } 
    } 

    public static ConnectionUtil getInstance() { 
     return instance; 
    } 

    public Connection getConnection() throws SQLException { 
     Connection connection = dataSource.getConnection(); 
     return connection; 
    } 

    public void close(Connection connection) throws SQLException { 
     if (connection != null && !connection.isClosed()) { 
      connection.close(); 
     } 
     connection = null; 
    } 

} 

,我使用JBoss AS 7.1與此的conf

<datasource jta="false" jndi-name="java:/dsFormationJSP" pool-name="dsFormationJSP" enabled="true" use-ccm="false"> 
        <connection-url>jdbc:mysql://localhost:3306/base_personne</connection-url> 
        <driver-class>com.mysql.jdbc.Driver</driver-class> 
        <driver>mysql-connector-java-5.1.30-bin.jarcom.mysql.jdbc.Driver_5_1</driver> 
        <pool> 
         <min-pool-size>50</min-pool-size> 
         <max-pool-size>70</max-pool-size> 
         <prefill>true</prefill> 
        </pool> 
        <security> 
         <user-name>root</user-name> 
         <password>root</password> 
        </security> 
        <validation> 
         <validate-on-match>false</validate-on-match> 
         <background-validation>false</background-validation> 
        </validation> 
        <timeout> 
         <blocking-timeout-millis>50000</blocking-timeout-millis> 
         <idle-timeout-minutes>5</idle-timeout-minutes> 
        </timeout> 
        <statement> 
         <share-prepared-statements>false</share-prepared-statements> 
        </statement> 
       </datasource> 

當我同步的兩種方法是確定。我通常不需要(數據庫和jboss的本地機制)。感謝您的幫助。

+0

請用適當的語言標記,這裏是'java',這樣你的代碼和響應者的代碼就可以適當地對顏色格式化。 – Parfait

+1

同步哪2種方法? – gerrytan

回答

0

它看起來像connectiongetAllPersonnes()是一個字段,而不是一個局部變量。這意味着當併發運行時,一個執行可能會覆蓋另一個執行的連接,這可能導致兩個執行使用相同的連接。

例如:

  • 線程1connection
  • 線程2connection
  • 線程1準備語句(帶連接由線程2設置!),
  • T hread2準備發言,

無論採用哪種方法完成第一關是用它做之前,其他的連接。關閉連接會關閉從屬語句和結果集,這會導致錯誤。

您需要將connection更改爲方法內部的局部變量,因此不能被其他線程覆蓋。

+0

謝謝。我更改了代碼,並在我的方法中獲得了我的連接: \t連接\t connection = \t ConnectionUtil.getInstance()。getConnection(); –