mybatis报错 Error configuring AutoCommit. Your driver may not support getAutoCommit() or setAutoCommi

报错信息

== Error configuring AutoCommit. Your driver may not support getAutoCommit() or setAutoCommit(). Requested setting: false. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure==

在这里插入图片描述

15:59:07.225 [http-bio-8080-exec-5] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
15:59:07.228 [http-bio-8080-exec-5] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Checked out connection 769784976 from pool.
15:59:07.232 [http-bio-8080-exec-5] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@2de1fc90]
java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at edu.finance.web.servlet.BaseServlet.service(BaseServlet.java:43)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: org.apache.ibatis.transaction.TransactionException: Error configuring AutoCommit.  Your driver may not support getAutoCommit() or setAutoCommit(). Requested setting: false.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 150,621 milliseconds ago.  The last packet sent successfully to the server was 64 milliseconds ago.
### The error may exist in edu/finance/mapper/BrandMapper.xml
### The error may involve edu.finance.mapper.BrandMapper.deleteByIds
### The error occurred while executing an update
### Cause: org.apache.ibatis.transaction.TransactionException: Error configuring AutoCommit.  Your driver may not support getAutoCommit() or setAutoCommit(). Requested setting: false.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 150,621 milliseconds ago.  The last packet sent successfully to the server was 64 milliseconds ago.
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:199)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.delete(DefaultSqlSession.java:212)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:72)
	at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:152)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)
	at com.sun.proxy.$Proxy31.deleteByIds(Unknown Source)
	at edu.finance.service.Impl.BrandServiceImpl.deleteByIds(BrandServiceImpl.java:59)
	at edu.finance.web.servlet.BrandServlet.deleteByIds(BrandServlet.java:67)
	... 25 more
Caused by: org.apache.ibatis.transaction.TransactionException: Error configuring AutoCommit.  Your driver may not support getAutoCommit() or setAutoCommit(). Requested setting: false.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 150,621 milliseconds ago.  The last packet sent successfully to the server was 64 milliseconds ago.
	at org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:108)
	at org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:143)
	at org.apache.ibatis.transaction.jdbc.JdbcTransaction.getConnection(JdbcTransaction.java:61)
	at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:337)
	at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:86)
	at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:49)
	at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
	at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197)
	... 32 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

原因

根据博客 https://blog.csdn.net/weixin_42492886/article/details/112213743

应该是长时间不连接导致报错

解决

在配置文件mybatis-config.xml中加入如下配置:

    <property name="poolPingQuery" value="SELECT NOW()" />
    <property name="poolPingEnabled" value="true" />

完整版mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <typeAliases>
        <package name="edu.finance.pojo"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql:///db1?useSSL=false"/>
                <property name="username" value="xxx"/>
                <property name="password" value="xxx"/>
                <property name="poolPingQuery" value="SELECT NOW()" />
                <property name="poolPingEnabled" value="true" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
       <!--扫描mapper-->
        <package name="edu.finance.mapper"/>
    </mappers>
</configuration>

两个配置项说明

链接: 在mybatis官网

说明如下:
在这里插入图片描述

  • poolPingQuery – 发送到数据库的侦测查询,用来检验连接是否正常工作并准备接受请求。默认是“NO PING QUERY SET”,这会导致多数数据库驱动出错时返回恰当的错误消息。
  • poolPingEnabled – 是否启用侦测查询。若开启,需要设置 poolPingQuery 属性为一个可执行的 SQL 语句(最好是一个速度非常快的 SQL 语句),默认值:false
根据提供的引用内容,解决Mybatis报错org.apache.ibatis.exceptions.PersistenceException的方法如下: 1. 检查配置文件:首先,确保你的Mybatis配置文件正确无误。检查是否存在语法错误、路径错误或其他配置错误。特别注意检查数据库连接信息是否正确。 2. 检查SQL语句:如果报错信息中包含"Error querying",则可能是SQL语句的问题。检查你的SQL语句是否正确,包括表名、列名、参数等是否与数据库中的实际情况一致。 3. 检查映射文件:如果使用了映射文件,检查映射文件中的配置是否正确。确保映射文件中的命名空间、SQL语句的id、参数等与实际情况一致。 4. 检查数据库连接:如果报错信息中包含"Error querying",还需要检查数据库连接是否正常。确保数据库服务已启动,并且数据库连接信息正确。 5. 查看详细错误信息:如果以上方法都无法解决问题,可以查看详细的错误信息,以便更好地定位问题。可以通过打印异常堆栈信息或查看日志文件来获取更多的错误信息。 6. 检查依赖:如果以上方法都无法解决问题,还可以检查你的项目依赖是否正确。确保你的项目中包含了正确版本的Mybatis依赖。 下面是一个示例代码,演示了如何解决Mybatis报错org.apache.ibatis.exceptions.PersistenceException: ```java try { // 执行Mybatis操作 } catch (PersistenceException e) { // 处理异常 e.printStackTrace(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值