在SAE使用spring+druid连接数据库错误处理的问题

很多人反映使用spring + druid连接SAE的共享型数据库总是报以下的错误:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataAccessResourceFailureException:  
### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 6,370,147 milliseconds ago.  The last packet sent successfully to the server was 2 milliseconds ago.  
### The error may exist in mapper/BookInfoMapper.xml
### The error may involve bookMapper.getBookInfo-Inline
### The error occurred while setting parameters
### SQL: select        bookname,   photo,   video,   num      from     book
### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 6,370,147 milliseconds ago.  The last packet sent successfully to the server was 2 milliseconds ago.  
; SQL []; Communications link failure

The last packet successfully received from the server was 6,370,147 milliseconds ago.  The last packet sent successfully to the server was 2 milliseconds ago.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 6,370,147 milliseconds ago.  The last packet sent successfully to the server was 2 milliseconds ago.  
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:681)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    org.apache.logging.log4j.core.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:66)

经过排查是由于使用的druid版本过低导致,请使用最新版本的druid,jar包下载地址:https://mvnrepository.com/artifact/com.alibaba/druid/1.1.2

推荐配置:

services-context.xml配置

<!-- 数据源配置 -->  
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <!-- 基本属性 url、user、password -->
        <!-- - <property name="driver" value="${jdbc_driver}" /> -->
        <property name="url" value="${jdbc_url}" />
        <property name="username" value="${jdbc_username}" />
        <property name="password" value="${jdbc_password}" />

        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="${druid_initialSize}" />
        <property name="minIdle" value="${druid_minIdle}" />
        <property name="maxActive" value="${druid_maxActive}" />

        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="${druid_maxWait}" />

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${druid_timeBetweenEvictionRunsMillis}" />

        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${druid_minEvictableIdleTimeMillis}" />
        <!-- 
        <property name="validationQuery" value="${druid_validationQuery}" />
         -->
        <property name="testWhileIdle" value="${druid_testWhileIdle}" />
        <property name="testOnBorrow" value="${druid_testOnBorrow}" />
        <property name="testOnReturn" value="${druid_testOnReturn}" />

        <!-- 打开PSCache,并且指定每个连接上PSCache的大小
        <property name="poolPreparedStatements" value="${druid_poolPreparedStatements}" />

        <property name="maxPoolPreparedStatementPerConnectionSize"
            value="${druid_maxPoolPreparedStatementPerConnectionSize}" />
        --> 
        <!-- 配置监控统计拦截的filters -->
        <!-- <property name="filters" value="${druid_filters}" /> -->
    </bean>

jdbc.properties中推荐配置:

druid_initialSize=3  
druid_minIdle=3  
druid_maxActive=10  
druid_maxWait=10000  
druid_timeBetweenEvictionRunsMillis=10000  
druid_minEvictableIdleTimeMillis=10000

druid_validationQuery="SELECT 1"  
druid_testWhileIdle=true  
#druid_testOnBorrow=false
druid_testOnReturn=false  
druid_testOnBorrow=true  
druid_keepAlive=true

druid_poolPreparedStatements=false  
druid_maxPoolPreparedStatementPerConnectionSize=20  
druid.filters=stat