在照抄张老师的秒杀系统的时候, 运行junit测试函数testQueryById, 遇到"java.sql.SQLException: An attempt by a client to checkout a Connection has timed out."问题. 折腾了两个小时解决. 总结如下.
1. 首先可以通过mysql的客户端排除SecKillDao.xml本身错误
2. 在tracestack提示的exception函数(translateExceptionIfPossible)添加断点. 然后debug as junit4.
神奇的发现, console打印了更具体的错误信息(见表2: 更具体的错误信息), 这时在百度这个错误信息就很容易知道如何解决.
即在jdbc.url后面添加&serverTimezone=UTC.
最终, 详细的jdbc.properties配置如下:
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/seckill?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=serverTimezone=Asia/Shanghai jdbc.username=root jdbc.password=xxxxxx |
原始错误信息: junit失败的tracestack如下: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: An attempt by a client to checkout a Connection has timed out. ### The error may exist in file [E:\server_project\seckill\target\classes\mapper\SecKillDao.xml]### The error may involve org.seckill.dao.SeckillDao.queryById ### The error occurred while executing a query ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: An attempt by a client to checkout a Connection has timed out. at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446) Console打印如下: 五月 06, 2018 10:49:39 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getDefaultTestExecutionListenerClassNames |
表2: 更具体的错误信息 警告: com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@1385856 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (2). Last acquisition attempt exception: java.sql.SQLException: The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:545) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513) |