qq_36648435 2018-03-15 08:24 采纳率: 0%
浏览 1129
已结题

spring-mybatis.xml而配置druid连接池错误

package gg;

public class hj {
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:context=\"http://www.springframework.org/schema/context\" xmlns:tx=\"http://www.springframework.org/schema/tx\"
xmlns:aop=\"http://www.springframework.org/schema/aop\"
xsi:schemaLocation=\"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
\">
<!--自动扫描-->


/context:component-scan

    <context:property-placeholder ignore-unresolvable=\"true\" location=\"classpath:jdbc.properties\"/>
    <!--============================配置数据源=================================-->
    <bean id=\"dataSource\" class=\"com.alibaba.druid.pool.DruidDataSource\" init-method=\"init\"
          destroy-method=\"close\">
        <property name=\"driverClassName\" value=\"${driverClass}\"/>
        <property name=\"url\" value=\"${jdbcurl}\"/>
        <property name=\"username\" value=\"${jdbcusername}\"/>
        <property name=\"password\" value=\"${jdbcpassword}\"/>
        <!-- 初始化连接大小 -->
        <property name=\"initialSize\" value=\"2\"/>
        <!-- 连接池最大使用连接数量 -->
        <property name=\"maxActive\" value=\"${maxActive}\"/>
        <!-- 连接池最大空闲 -->
        <!-- <property name=\"maxIdle\" value=\"20\" /> -->
        <!-- 连接池最小空闲 -->
        <property name=\"minIdle\" value=\"${minIdle}\"/>
        <!-- 获取连接最大等待时间 -->
        <property name=\"maxWait\" value=\"${maxWait}\"/>
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name=\"timeBetweenEvictionRunsMillis\" value=\"60000\"/>
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name=\"minEvictableIdleTimeMillis\" value=\"25200000\"/>

        <!-- 打开removeAbandoned功能 -->
        <property name=\"removeAbandoned\" value=\"true\"/>
        <!-- 1800秒,也就是30分钟 -->
        <property name=\"removeAbandonedTimeout\" value=\"1800\"/>
        <!-- 关闭abanded连接时输出错误日志 -->
        <property name=\"logAbandoned\" value=\"true\"/>
        <!-- 监控数据库 -->
        <!-- <property name=\"filters\" value=\"stat\" /> -->
        <property name=\"filters\" value=\"mergeStat\"/>
    </bean>

    <!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 -->
    <bean id=\"sqlSessionFactory\" class=\"org.mybatis.spring.SqlSessionFactoryBean\">
        <property name=\"dataSource\" ref=\"dataSource\"/>
        <!-- 自动扫描mapping.xml文件 -->
        <property name=\"mapperLocations\" value=\"classpath:/mapper/*.xml\"/>
        <!--  分页查询(mybatis 3.2.8以上版本)-张晗 -->
        <property name=\"plugins\">
            <array>
                <bean class=\"com.github.pagehelper.PageInterceptor\">
                    <property name=\"properties\">
                        <value>
                            helperDialect=mysql
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>


    <!--DAO接口所在包名,Spring会自动查找其下的类-->
    <bean class=\"org.mybatis.spring.mapper.MapperScannerConfigurer\">
        <!--制定扫描的包名扫描dao层下的类-->
        <property name=\"basePackage\" value=\"com.books.dao\"/>
        <property name=\"sqlSessionFactoryBeanName\" value=\"sqlSessionFactory\"/>
    </bean>

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id=\"transactionManager\" class=\"org.springframework.jdbc.datasource.DataSourceTransactionManager\">
        <property name=\"dataSource\" ref=\"dataSource\"/>
    </bean>
    <!--使用注释事务 -->
    <tx:annotation-driven transaction-manager=\"transactionManager\"/>

   <!--  <import resource=\"spring-schedule.xml\"/> -->

</beans>



15-Mar-2018 16:01:27.914 严重 [RMI TCP Connection(5)-127.0.0.1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [spring-mybatis.xml]: Invocation of init method failed; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
  • 写回答

4条回答

  • eagler 2018-03-15 08:57
    关注

    可以肯定的是数据库连接失败导致dataSource创建失败,你可以根据jdbc.properties,用命令行或者连接工具登录试一下就知道问题在哪了

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘