简体   繁体   English

Jmeter测试的JDCBC连接错误

[英]JDCBC connection error with Jmeter test

I try to stress my application with Jmeter. 我尝试使用Jmeter来强调我的应用程序。 When i don't synchronize my two methods I have multiple errors like the following: 当我不同步我的两个方法时,我将遇到多个错误,如下所示:

The statement is closed. 该语句已关闭。 or 要么

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)

or 要么

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

or 要么

 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)

my code : 我的代码:

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;

    }

}

and

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;
    }

}

and i use Jboss AS 7.1 with this conf 我在这个会议上使用Jboss AS 7.1

  <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>

When i synchronized the two methods it's OK. 当我同步这两种方法时,就可以了。 I normally do not need to to that ( its the native mechanism of database and jboss).Thanks for your help. 我通常不需要这样做(它是数据库和jboss的本机机制)。谢谢您的帮助。

It looks like connection in getAllPersonnes() is a field, and not a local variable. 看起来getAllPersonnes()中的connection是一个字段,而不是局部变量。 This means that when run concurrently one execution could overwrite the connection of another execution, this can lead to two executions using the same connection. 这意味着当同时执行一个执行可能会覆盖另一个执行的连接时,这可能导致使用同一连接的两个执行。

For example: 例如:

  • Thread1 sets connection , Thread1设置connection
  • Thread2 sets connection , Thread2设置connection
  • Thread1 prepares statement (with connection set by Thread2 !), 线程1准备语句(带连接由线程2集!),
  • Thread2 prepares statement, Thread2准备语句,
  • etc 等等

Whichever method finishes first closes the connection before the other is done with it. 无论哪种方法先完成,都要先关闭连接,然后再完成另一种方法。 Closing a connection closes the dependent statement and result set, which leads to the errors. 关闭连接会关闭从属语句和结果集,从而导致错误。

You need to change connection to a local variable inside the method, so it can't be overwritten by other threads. 您需要更改与方法内部的局部变量的connection ,因此它不能被其他线程覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM