首页 新闻 会员 周边 捐助

java中ssm框架数据库连接不上

0
[已关闭问题] 关闭于 2018-03-07 15:08

十一月 17, 2017 8:18:21 上午 org.apache.catalina.startup.Catalina start
信息: Server startup in 25961 ms
十一月 17, 2017 8:19:20 上午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [springMVC] in context with path [/Exercise01] threw exception [Request processing failed; nested exception is 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: Connections could not be acquired from the underlying database!
### The error may exist in com/mapper/userMapper.xml
### The error may involve com.mapper.UserMapper.findAge
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!] with root cause
com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1418)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:606)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:526)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutAndMarkConnectionInUse(C3P0PooledConnectionPool.java:755)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:682)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:84)
at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:70)
at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:336)
at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:84)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62)
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.CachingExecutor.query(CachingExecutor.java:109)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:434)
at com.sun.proxy.$Proxy9.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:167)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:82)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
at com.sun.proxy.$Proxy16.findAge(Unknown Source)
at com.service.impl.UserServiceImpl.findAge(UserServiceImpl.java:18)
at com.controller.UserControllers.find(UserControllers.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

------------------------------------------------------------------------------------------

applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" 
 3     xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xmlns:mvc="http://www.springframework.org/schema/mvc"
 8     xmlns:tx="http://www.springframework.org/schema/tx"
 9     xsi:schemaLocation="http://www.springframework.org/schema/beans 
10                         http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
11                         http://www.springframework.org/schema/context
12                         http://www.springframework.org/schema/context/spring-context-4.2.xsd
13                         http://www.springframework.org/schema/mvc
14                         http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
15                         http://www.springframework.org/schema/tx
16                         http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
17                         http://mybatis.org/schema/mybatis-spring 
18                         http://mybatis.org/schema/mybatis-spring.xsd ">
19 
20     <!-- 使用PropertyOverrideConfigurer后处理器加载数据源参数 -->
21     <context:property-override location="classpath:config/db.properties"/>
22     
23     <!-- 1. c3p0数据源 :-->
24     <!-- <bean id="dataSource"
25         class="com.mchange.v2.c3p0.ComboPooledDataSource">
26         <property name="driverClass" value="com.mysql.jdbc.Driver" />
27         <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm" />
28         <property name="user" value="root" />
29         <property name="password" value="root" />
30     </bean> -->
31     <bean id="dataSource"
32         class="com.mchange.v2.c3p0.ComboPooledDataSource">
33         <property name="driverClass" value="${dataSource.driverClass}" />
34         <property name="jdbcUrl" value="${dataSource.jdbcUrl}" />
35         <property name="user" value="${dataSource.user}" />
36         <property name="password" value="${dataSource.password}" />
37     </bean>
38     <!--
39         2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源
40         MyBatis定义数据源,同意加载配置
41     -->
42     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
43         <property name="dataSource" ref="dataSource"></property>
44         <property name="configLocation" value="classpath:config/mybatis-config.xml" /> 
45     </bean>
46 
47     <!--
48         3. mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory
49 
50         basePackage:指定sql映射文件/接口所在的包(自动扫描)
51     -->
52     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
53         <property name="basePackage" value="com.mapper"></property>
54         <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
55     </bean>
56 
57     <!--
58         4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源
59     -->
60     <bean id="txManager"
61         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
62         <property name="dataSource" ref="dataSource"></property>
63     </bean>
64 
65     <!-- 5. 使用声明式事务
66          transaction-manager:引用上面定义的事务管理器
67      -->
68     <tx:annotation-driven transaction-manager="txManager" />
69 
70 </beans>

db.properties

复制代码
1 dataSource.driverClass=com.mysql.jdbc.Driver
2 dataSource.jdbcUrl=jdbc:mysql://localhost:3306/ssm
3 dataSource.user=root
4 dataSource.password=root
5 dataSource.maxPoolSize=20
6 dataSource.maxIdleTime = 1000
7 dataSource.minPoolSize=6
8 dataSource.initialPoolSize=5
复制代码
冷月无情的主页 冷月无情 | 菜鸟二级 | 园豆:206
提问于:2017-11-20 11:29
< >
分享
所有回答(2)
0

Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!

ycyzharry | 园豆:25683 (高人七级) | 2017-11-20 19:14

这位大神遇到这种情况该怎么解决呢?

支持(0) 反对(0) 冷月无情 | 园豆:206 (菜鸟二级) | 2017-11-21 10:57

@冷月无情: 看看pom.xml 中,下面这个依赖加了没

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.38</version>
</dependency>

 

 

支持(0) 反对(0) ycyzharry | 园豆:25683 (高人七级) | 2017-11-21 13:15

@ycyzharry: 我的jar包有这个

支持(0) 反对(0) 冷月无情 | 园豆:206 (菜鸟二级) | 2017-11-21 14:03
0

1、确认一下数据库配置信息是否正确

2、数据库是否有访问权限

3、驱动对应的jar包是否引入

一腿狗毛 | 园豆:326 (菜鸟二级) | 2017-11-21 14:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册