Error accessing PooledConnection. Connection is invalid

    public static void main(String[] args) {
        final CountDownLatch latch = new CountDownLatch(1);
        final int threadCount = 10;

        for (int i = 0; i < threadCount; i++) {
            new Thread() {

                @Override
                public void run() {
                    try {
                        // all thread to wait
                        latch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    // do something
                    task();
                }
            }.start();
        }
        // release lock, let all thread excute at the same time
        latch.countDown();
    }

    private static void task() {
        SqlSession session = Singleton.INSTANCE.getMybatisFactory().openSession();
        try {
            InfoMapper mapper = session.getMapper(InfoMapper.class);
                        
            mapper.getNews(1);

            PageHelper.startPage(1, 10);
            mapper.listIndustryClass();
        } finally {
            session.close();
        }
    }

执行并发测试,10条线程,报如下错误

### Cause: java.sql.SQLException: Error accessing PooledConnection. Connection is invalid.
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy20.listIndustryClass(Unknown Source)
at jinxiu_m.Test.task(Test.java:66)
at jinxiu_m.Test.access$0(Test.java:58)
at jinxiu_m.Test$1.run(Test.java:47)
Caused by: java.sql.SQLException: Error accessing PooledConnection. Connection is invalid.
at org.apache.ibatis.datasource.pooled.PooledConnection.checkConnection(PooledConnection.java:254)
at org.apache.ibatis.datasource.pooled.PooledConnection.invoke(PooledConnection.java:243)
at com.sun.proxy.$Proxy21.prepareStatement(Unknown Source)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.instantiateStatement(PreparedStatementHandler.java:87)
at org.apache.ibatis.executor.statement.BaseStatementHandler.prepare(BaseStatementHandler.java:88)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.prepare(RoutingStatementHandler.java:59)
at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:85)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:112)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61)
at com.sun.proxy.$Proxy19.query(Unknown Source)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
... 8 more

如果

        PageHelper.startPage(1, 10);
        mapper.listIndustryClass();

        mapper.getNews(1);

或者

//          mapper.getNews(1);
        
        PageHelper.startPage(1, 10);
        mapper.listIndustryClass();

则不会报错,原因待查。。。

你可能感兴趣的:(Error accessing PooledConnection. Connection is invalid)