Spring myBatis数据库连接异常问题及解决

 更新时间:2022年06月06日 09:39:08   作者:vera_vera_vera  
这篇文章主要介绍了Spring myBatis数据库连接异常问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

spring myBatis数据库连接异常

报错如下

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get 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:\Code_zxt\MVNWebDemo\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: Could not get 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:76)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:399)
at com.sun.proxy.$Proxy13.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:165)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:69)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
at com.sun.proxy.$Proxy14.queryById(Unknown Source)
at org.seckill.dao.SeckillDaoTest.TestQueryById(SeckillDaoTest.java:38)
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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.apache.ibatis.exceptions.PersistenceException: 

首先去看mysql服务是否启动,发现启动正常。

再去看jdbc.properties文件里面url username等写对没有,写的是正确的,但是发现password是unused。去看spring-dao.xml(spring和mybatis相关的配置文件),发现漏写了password的“}”

如下:

myBatis连接数据库时报错原因归纳

报错信息

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
localhost:1521:oracl


### The error may exist in sqlMapper/UsersMapper.xml
### The error may involve test.findUserById
### The error occurred while executing a query
### Cause: java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
localhost:1521:oracl

这里报错的可能性在于配置文件中的jdbc.url=jdbc:oracle:thin:@localhost:1521:oracl的oracl不是Oracle数据库的SID,数据库的SID是orcl,所以会报这个错,可以通过服务查看Oracle的SID进行核对 

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • java8如何根据list对象中的属性过滤筛选

    java8如何根据list对象中的属性过滤筛选

    这篇文章主要介绍了java8如何根据list对象中的属性过滤筛选,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • java中超过long范围的超大整数相加算法详解(面试高频)

    java中超过long范围的超大整数相加算法详解(面试高频)

    这篇文章主要介绍了java中超过long范围的超大整数相加算法(面试高频),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • Java中获取子字符串的几种方法示例

    Java中获取子字符串的几种方法示例

    这篇文章主要主要给大家总结了Java中获取子字符串的几种方法,分别是采用split的方式、采用indexOf的方式、正则和采用replaceFirst的方式这四种方法,需要的朋友可以参考借鉴,下面来看看详细的介绍吧
    2017-01-01
  • springboot prototype设置多例不起作用的解决操作

    springboot prototype设置多例不起作用的解决操作

    这篇文章主要介绍了springboot prototype设置多例不起作用的解决操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • springboot多文件或者文件夹压缩成zip的方法

    springboot多文件或者文件夹压缩成zip的方法

    最近碰到个需要下载zip压缩包的需求,于是我在网上找了下别人写好的zip工具类,下面通过本文给大家分享springboot多文件或者文件夹压缩成zip的方法,感兴趣的朋友一起看看吧
    2024-07-07
  • SpringBoot中的Controller用法示例详解

    SpringBoot中的Controller用法示例详解

    Controller是SpringBoot里最基本的组件,他的作用是把用户提交来的请求通过对URL的匹配,分配给不同的接收器,再进行处理,然后向用户返回结果,这篇文章主要介绍了SpringBoot中的Controller用法,需要的朋友可以参考下
    2023-06-06
  • java使用socket实现一个多线程web服务器的方法

    java使用socket实现一个多线程web服务器的方法

    今天小编就为大家分享一篇java使用socket实现一个多线程web服务器的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • Java创建多线程异步执行实现代码解析

    Java创建多线程异步执行实现代码解析

    这篇文章主要介绍了Java创建多线程异步执行实现代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • 详解Spring Boot微服务如何集成fescar解决分布式事务问题

    详解Spring Boot微服务如何集成fescar解决分布式事务问题

    这篇文章主要介绍了详解Spring Boot微服务如何集成fescar解决分布式事务问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • java固定大小队列的几种实现方式详解

    java固定大小队列的几种实现方式详解

    队列的特点是节点的排队次序和出队次序按入队时间先后确定,即先入队者先出队,后入队者后出队,这篇文章主要给大家介绍了关于java固定大小队列的几种实现方式,需要的朋友可以参考下
    2021-07-07

最新评论