ssm框架,无法访问数据库Could not get JDBC Connection 5C

用的德鲁伊连接池,先看applicationContext-dao.xml里的配置

 <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <context:property-placeholder location="classpath:properties/*.properties" />

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close">
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="maxActive" value="10" />
        <property name="minIdle" value="5" />
        <!-- 
        <property name="maxWait" value="5000" /> -->

    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.demo.mapper" />
    </bean>
</beans>

这样的话,控制台输出:
2017-12-28 12:43:58,209DEBUG[org.springframework.web.servlet.DispatcherServlet] -
2017-12-28 12:43:58,214DEBUG[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] -
2017-12-28 12:43:58,221DEBUG[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] -
2017-12-28 12:43:58,221DEBUG[org.springframework.beans.factory.support.DefaultListableBeanFactory] -
2017-12-28 12:43:58,222DEBUG[org.springframework.web.servlet.DispatcherServlet] -
2017-12-28 12:43:58,317DEBUG[org.mybatis.spring.SqlSessionUtils] -
2017-12-28 12:43:58,335DEBUG[org.mybatis.spring.SqlSessionUtils] -
2017-12-28 12:43:58,366DEBUG[org.springframework.jdbc.datasource.DataSourceUtils] -
2017-12-28 12:43:58,447INFO[com.alibaba.druid.pool.DruidDataSource] - <{dataSource-1} inited>

一直显示等待,后来加了个maxWait属性,时间是5秒,然后才开始报错,如下:
2017-12-28 12:47:41,261DEBUG[org.springframework.web.servlet.DispatcherServlet] -
2017-12-28 12:47:41,264DEBUG[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] -
2017-12-28 12:47:41,269DEBUG[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] -
2017-12-28 12:47:41,269DEBUG[org.springframework.beans.factory.support.DefaultListableBeanFactory] -
2017-12-28 12:47:41,270DEBUG[org.springframework.web.servlet.DispatcherServlet] -
2017-12-28 12:47:41,338DEBUG[org.mybatis.spring.SqlSessionUtils] -
2017-12-28 12:47:41,348DEBUG[org.mybatis.spring.SqlSessionUtils] -
2017-12-28 12:47:41,378DEBUG[org.springframework.jdbc.datasource.DataSourceUtils] -
2017-12-28 12:47:41,472INFO[com.alibaba.druid.pool.DruidDataSource] - <{dataSource-1} inited>
2017-12-28 12:47:46,473WARN[com.alibaba.druid.pool.DruidDataSource] -
2017-12-28 12:47:51,476DEBUG[org.mybatis.spring.SqlSessionUtils] -
2017-12-28 12:47:51,476DEBUG[org.mybatis.spring.SqlSessionUtils] -
2017-12-28 12:47:51,476DEBUG[org.mybatis.spring.SqlSessionUtils] -
2017-12-28 12:47:51,476DEBUG[org.springframework.jdbc.datasource.DataSourceTransactionManager] -
2017-12-28 12:47:51,478DEBUG[org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver] - <Resolving exception from handler [public com.demo.pojo.TbItem com.demo.controller.TestController.getItem(java.lang.Long)]: 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 com.alibaba.druid.pool.GetConnectionTimeoutException: wait millis 5000, active 0

The error may exist in com/demo/mapper/TbItemMapper.xml

The error may involve com.demo.mapper.TbItemMapper.selectByPrimaryKey

The error occurred while executing a query

无法连接到数据库。。。。错误显示可能在/TbItemMapper.xml,这是mybatis逆向生成的,在其他项目中测试可用,所以问题应该不在这,其他配置文件啥的也感觉没问题,但就是连接不到数据库,问题出在哪???

TbItemMapper.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.mapper.TbItemMapper">
  <resultMap id="BaseResultMap" type="com.demo.pojo.TbItem">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="title" jdbcType="VARCHAR" property="title" />
    <result column="sell_point" jdbcType="VARCHAR" property="sellPoint" />
    <result column="price" jdbcType="BIGINT" property="price" />
    <result column="num" jdbcType="INTEGER" property="num" />
    <result column="barcode" jdbcType="VARCHAR" property="barcode" />
    <result column="image" jdbcType="VARCHAR" property="image" />
    <result column="cid" jdbcType="BIGINT" property="cid" />
    <result column="status" jdbcType="TINYINT" property="status" />
    <result column="created" jdbcType="TIMESTAMP" property="created" />
    <result column="updated" jdbcType="TIMESTAMP" property="updated" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, title, sell_point, price, num, barcode, image, cid, status, created, updated
  </sql>
  <select id="selectByExample" parameterType="com.demo.pojo.TbItemExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from tb_item
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from tb_item
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from tb_item
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="com.demo.pojo.TbItemExample">
    delete from tb_item
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.demo.pojo.TbItem">
    insert into tb_item (id, title, sell_point, 
      price, num, barcode, 
      image, cid, status, 
      created, updated)
    values (#{id,jdbcType=BIGINT}, #{title,jdbcType=VARCHAR}, #{sellPoint,jdbcType=VARCHAR}, 
      #{price,jdbcType=BIGINT}, #{num,jdbcType=INTEGER}, #{barcode,jdbcType=VARCHAR}, 
      #{image,jdbcType=VARCHAR}, #{cid,jdbcType=BIGINT}, #{status,jdbcType=TINYINT}, 
      #{created,jdbcType=TIMESTAMP}, #{updated,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" parameterType="com.demo.pojo.TbItem">
    insert into tb_item
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="title != null">
        title,
      </if>
      <if test="sellPoint != null">
        sell_point,
      </if>
      <if test="price != null">
        price,
      </if>
      <if test="num != null">
        num,
      </if>
      <if test="barcode != null">
        barcode,
      </if>
      <if test="image != null">
        image,
      </if>
      <if test="cid != null">
        cid,
      </if>
      <if test="status != null">
        status,
      </if>
      <if test="created != null">
        created,
      </if>
      <if test="updated != null">
        updated,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="title != null">
        #{title,jdbcType=VARCHAR},
      </if>
      <if test="sellPoint != null">
        #{sellPoint,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        #{price,jdbcType=BIGINT},
      </if>
      <if test="num != null">
        #{num,jdbcType=INTEGER},
      </if>
      <if test="barcode != null">
        #{barcode,jdbcType=VARCHAR},
      </if>
      <if test="image != null">
        #{image,jdbcType=VARCHAR},
      </if>
      <if test="cid != null">
        #{cid,jdbcType=BIGINT},
      </if>
      <if test="status != null">
        #{status,jdbcType=TINYINT},
      </if>
      <if test="created != null">
        #{created,jdbcType=TIMESTAMP},
      </if>
      <if test="updated != null">
        #{updated,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.demo.pojo.TbItemExample" resultType="java.lang.Long">
    select count(*) from tb_item
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update tb_item
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.title != null">
        title = #{record.title,jdbcType=VARCHAR},
      </if>
      <if test="record.sellPoint != null">
        sell_point = #{record.sellPoint,jdbcType=VARCHAR},
      </if>
      <if test="record.price != null">
        price = #{record.price,jdbcType=BIGINT},
      </if>
      <if test="record.num != null">
        num = #{record.num,jdbcType=INTEGER},
      </if>
      <if test="record.barcode != null">
        barcode = #{record.barcode,jdbcType=VARCHAR},
      </if>
      <if test="record.image != null">
        image = #{record.image,jdbcType=VARCHAR},
      </if>
      <if test="record.cid != null">
        cid = #{record.cid,jdbcType=BIGINT},
      </if>
      <if test="record.status != null">
        status = #{record.status,jdbcType=TINYINT},
      </if>
      <if test="record.created != null">
        created = #{record.created,jdbcType=TIMESTAMP},
      </if>
      <if test="record.updated != null">
        updated = #{record.updated,jdbcType=TIMESTAMP},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update tb_item
    set id = #{record.id,jdbcType=BIGINT},
      title = #{record.title,jdbcType=VARCHAR},
      sell_point = #{record.sellPoint,jdbcType=VARCHAR},
      price = #{record.price,jdbcType=BIGINT},
      num = #{record.num,jdbcType=INTEGER},
      barcode = #{record.barcode,jdbcType=VARCHAR},
      image = #{record.image,jdbcType=VARCHAR},
      cid = #{record.cid,jdbcType=BIGINT},
      status = #{record.status,jdbcType=TINYINT},
      created = #{record.created,jdbcType=TIMESTAMP},
      updated = #{record.updated,jdbcType=TIMESTAMP}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.demo.pojo.TbItem">
    update tb_item
    <set>
      <if test="title != null">
        title = #{title,jdbcType=VARCHAR},
      </if>
      <if test="sellPoint != null">
        sell_point = #{sellPoint,jdbcType=VARCHAR},
      </if>
      <if test="price != null">
        price = #{price,jdbcType=BIGINT},
      </if>
      <if test="num != null">
        num = #{num,jdbcType=INTEGER},
      </if>
      <if test="barcode != null">
        barcode = #{barcode,jdbcType=VARCHAR},
      </if>
      <if test="image != null">
        image = #{image,jdbcType=VARCHAR},
      </if>
      <if test="cid != null">
        cid = #{cid,jdbcType=BIGINT},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=TINYINT},
      </if>
      <if test="created != null">
        created = #{created,jdbcType=TIMESTAMP},
      </if>
      <if test="updated != null">
        updated = #{updated,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.demo.pojo.TbItem">
    update tb_item
    set title = #{title,jdbcType=VARCHAR},
      sell_point = #{sellPoint,jdbcType=VARCHAR},
      price = #{price,jdbcType=BIGINT},
      num = #{num,jdbcType=INTEGER},
      barcode = #{barcode,jdbcType=VARCHAR},
      image = #{image,jdbcType=VARCHAR},
      cid = #{cid,jdbcType=BIGINT},
      status = #{status,jdbcType=TINYINT},
      created = #{created,jdbcType=TIMESTAMP},
      updated = #{updated,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=BIGINT}
  </update>
</mapper>

上面是逆向工程的mapper,后来我自己写了个TestMapper,还是出现同样的问题:

图片说明

另外navicat访问数据库没问题。我想会不会是连接池配置的问题,也许那些配置可能导致这种情况??

qq_35685189
handsome-boy 你的这个问题解决了吗?
一年多之前 回复

7个回答

把 username 换个名字 再试试

com.demo.mapper.TbItemMapper.selectByPrimaryKey
你先把这个sql注释了再试试

1.确认数据库服务打开,并且使用工具可以连上。
2.再来看是不是代码的问题,这个提示是在Mapper文件中,把mapper贴出来看看。

Ivorybeach
王逍遥这是我们的狗 Navicat访问数据库没问题,mapper贴出来了
2 年多之前 回复

destroy-method="close">






<!--
-->

</bean>

    你把这些 跟你的那个 数据库的驱动文件对比下 看看有没有问题![图片说明](https://img-ask.csdn.net/upload/201712/28/1514441848_49807.jpg)  这个是我项目的配置  你可以参考下

猜测可能是主键生成的问题,不知道那个数据库,具体不好说

Ivorybeach
王逍遥这是我们的狗 用的别人的教学视频里的.sql文件,主键应该是一个自动生成id的通用工具类生成的,类型是long
2 年多之前 回复
Ivorybeach
王逍遥这是我们的狗 主键生成,可以详细说说吗?
2 年多之前 回复

The error may exist in com/demo/mapper/TbItemMapper.xml
The error may involve com.demo.mapper.TbItemMapper.selectByPrimaryKey
你把这个TbItemMapper.xml贴出来,看下selectByPrimaryKey有没有问题呢。

Ivorybeach
王逍遥这是我们的狗 mapper贴出来了,感觉没啥问题
2 年多之前 回复

不知道你解决没,我也遇到同样的问题,解决不了。找到你的帖子,后来我们改了 mysql 数据库的连接数(mysql 的自身的设置,和我们代码无关),由原来的 58 改到了 1000.就没有问题了。
看来是数据库连接数设置的太少,连接不够了导致获取不到数据库连接。

LiebeZQ
LiebeZQ 我还是没有解决,我看我的数据库最大连接数是100,正在连接的才5个,总是显示资源池被占用,连接不到数据库,烦躁啊
2 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐