无法针对 JDBC 连接提交

Posted

技术标签:

【中文标题】无法针对 JDBC 连接提交【英文标题】:Unable to commit against JDBC Connection 【发布时间】:2020-10-04 13:38:48 【问题描述】:

我正在使用springboot连接到一个mysql数据库。请在下面找到我的配置

spring.datasource.url=jdbc:<connection-url>
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true

spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.min-idle=15

API 代码

@CrossOrigin(origins = "*", allowedHeaders = "*")
@GetMapping(value = "/validateuser/consumerName")
@Transactional
public Boolean valiadateuser(@PathVariable String consumerName) 
    LOGGER.info("Inside validateuser -1");
    ConsumerName user = consumerRepository.findByName(consumerName);
    LOGGER.info("Inside validateuser -2 :::: " + user);
    if (user != null) 
        return Boolean.TRUE;
    
    return Boolean.FALSE;

以下是我的例外

 org.springframework.orm.jpa.JpaSystemException: Unable to commit against JDBC Connection; nested exception is org.hibernate.TransactionException: Unable to commit against JDBC Connection
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:353) ~[spring-orm-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255) ~[spring-orm-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:538) ~[spring-orm-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:743) [spring-tx-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:711) [spring-tx-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.completeTransactionAfterThrowing(TransactionAspectSupport.java:665) [spring-tx-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:370) [spring-tx-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) [spring-tx-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) [spring-aop-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691) [spring-aop-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
    at com.server.controller.SubscribeController$$EnhancerBySpringCGLIB$$14f090fd.subscribeTopic(<generated>) [classes!/:0.0.1-SNAPSHOT]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_251]

【问题讨论】:

为什么要加@Transactional 执行@transactional,不确保自动提交和自动关闭连接 @user7294900 我已经删除了注释,还是同样的问题 【参考方案1】:

找到答案

更新了 resource.properties

spring.datasource.url=jdbc:<connection-url>
spring.datasource.username=<username>
spring.datasource.password=<password>
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true

spring.jpa.hibernate.ddl-auto=update
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5

【讨论】:

【参考方案2】:

我在 Spring / Postgres 堆栈上遇到了同样的异常。基本上,数据库无法及时返回/提交与您的查询匹配的所有行。

可以通过在特定查询中使用的列上创建索引来修复它。这会加快查询速度。

CREATE INDEX index_redflag_person
ON redflag_person (firstname, alias,lastname,address,birthplace);

【讨论】:

以上是关于无法针对 JDBC 连接提交的主要内容,如果未能解决你的问题,请参考以下文章

JDBC处理事务与数据库连接池

JDBC 关闭数据库连接与自动提交

检测 JDBC 连接中尚未提交的打开事务

JDBC连接超时,针对连接不稳定,有时候能连上(登录),一会又报连接超时

JDBC 和 Spring 连接池

Presto JDBC 连接池创建错误“不支持禁用自动提交模式”