开发者社区> 问答> 正文

获取连接时java.sql.SQLException: interrupt

请帮忙看下,谢谢,druid1.1.5

Caused by: java.sql.SQLException: interrupt at com.alibaba.druid.pool.DruidDataSource.getConnectionInternal(DruidDataSource.java:1356) at com.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:1241) at com.alibaba.druid.filter.FilterChainImpl.dataSource_connect(FilterChainImpl.java:4619) at com.alibaba.druid.filter.stat.StatFilter.dataSource_getConnection(StatFilter.java:680) at com.alibaba.druid.filter.FilterChainImpl.dataSource_connect(FilterChainImpl.java:4615) at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1219) at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1211) at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:105) at com.dangdang.ddframe.rdb.sharding.jdbc.ShardingConnection.getConnectionInternal(ShardingConnection.java:107) at com.dangdang.ddframe.rdb.sharding.jdbc.ShardingConnection.getConnection(ShardingConnection.java:64) at com.dangdang.ddframe.rdb.sharding.jdbc.ShardingPreparedStatement.routeSQL(ShardingPreparedStatement.java:167) at com.dangdang.ddframe.rdb.sharding.jdbc.ShardingPreparedStatement.execute(ShardingPreparedStatement.java:115) at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:63) at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79) at sun.reflect.GeneratedMethodAccessor155.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63) at com.sun.proxy.$Proxy146.query(Unknown Source) at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63) 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.BaseExecutor.query(BaseExecutor.java:136) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141) at sun.reflect.GeneratedMethodAccessor394.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) 源码为DruidDataSource#getConnectionInternal try { lock.lockInterruptibly(); } catch (InterruptedException e) { connectErrorCount.incrementAndGet(); throw new SQLException("interrupt", e); }

已经把lock.lockInterruptibly()改成lock.lock()了, 请问这个是bug么? 怎么办?

原提问者GitHub用户hunterwangwang

展开
收起
山海行 2023-07-05 20:39:19 969 0
3 条回答
写回答
取消 提交回答
  • 这个异常通常发生在应用程序获取Druid连接池的连接过程中,该过程被线程中断所打断导致抛出java.sql.SQLException: interrupt异常。

    这个错误可以是以下几种情况之一:

    1. 线程中断:在获取连接的过程中,应用程序的线程被中断。例如,通过调用Thread.interrupt()方法中断了正在获取连接的线程。您可以检查代码中是否有地方对线程进行了中断操作。

    2. 连接超时设置过小:如果连接池的连接超时时间设置得太小,那么当应用程序的线程获取连接时,如果等待时间超过了设置的超时时间,也会抛出这个异常。您可以尝试增加连接超时时间。

    针对以上可能的情况,您可以采取以下措施来解决问题:

    - 检查代码:确保没有在获取连接的过程中中断了线程。检查相关代码,特别是涉及到线程中断的地方。

    - 调整连接超时时间:如果是因为连接超时导致的异常,您可以尝试增加连接超时时间,以允许更长的等待时间。

    - 检查应用程序其他部分:如果上述方法仍然无法解决问题,建议检查应用程序的其他部分,例如数据库连接的配置、网络状况等,以确定是否存在其他问题。

    2023-07-30 14:05:08
    赞同 展开评论 打赏
  • 北京阿里云ACE会长

    您的应用程序从Druid连接池获取连接时,如果获取连接的过程被中断,可能会抛出java.sql.SQLException: interrupt异常。这个异常通常是由于应用程序的线程在获取连接的过程中被中断,例如通过调用Thread.interrupt()方法中断线程。

    如果您在应用程序中使用了线程中断机制,并且出现了这个异常,可以尝试以下几种解决方法:

    禁用线程中断机制:如果您的应用程序中使用了线程中断机制,并且中断线程导致了连接获取异常,您可以尝试禁用线程中断机制。具体来说,您可以在获取连接之前调用Thread.currentThread().interrupted()方法来清除线程的中断状态,例如:

    Copy
    if (Thread.currentThread().isInterrupted()) {
    Thread.interrupted();
    }
    这样可以避免应用程序的线程在获取连接的过程中被中断,从而避免java.sql.SQLException: interrupt异常。

    调整连接池配置:如果您的应用程序在高并发情况下经常出现连接获取异常,可能是因为连接池的配置不合理,导致连接池无法满足应用程序的需求。您可以尝试调整连接池的配置参数,例如增加连接池的最大连接数、调整连接的最大等待时间等,以满足应用程序的需求。

    检查应用程序的其他部分:如果您的应用程序在使用Druid连接池时经常出现连接获取异常,可能是因为应用程序的其他部分存在问题,导致连接池无法正常工作。您可以检查应用程序的其他部分,例如数据库连接、SQL语句、事务处理等,以确定是否存在其他问题。

    2023-07-30 13:57:07
    赞同 展开评论 打赏
  • 申请连接的线程有调用interrupt,这个可以算bug,也可以不算哈

    原回答者GitHub用户wenshao

    2023-07-06 11:52:32
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
SQL Server 2017 立即下载
GeoMesa on Spark SQL 立即下载
原生SQL on Hadoop引擎- Apache HAWQ 2.x最新技术解密malili 立即下载