在SAE中使用druid连接共享数据库

因为SAE的共享型数据库超时时间是30s,只需要关注这一点。

pom.xml依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sinacloud.learn</groupId>
  <artifactId>myApp</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>myApp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.20</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.2</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>myApp</finalName>
  </build>
</project>  

下面的代码分别演示了不使用连接池连接和使用连接池的代码,其中注释的部分为不使用连接池。

package com.sinacloud.learn;

import java.io.IOException;  
import java.sql.SQLException;

import javax.servlet.ServletException;  
import javax.servlet.annotation.WebServlet;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.pool.DruidDataSource;  
import java.sql.*;

/**
 * Servlet implementation class Test
 */
@WebServlet("/Test")
public class Test extends HttpServlet {  
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public Test() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        /*String driver = "com.mysql.jdbc.Driver";
        String username = System.getenv("ACCESSKEY");
        String password = System.getenv("SECRETKEY");
        String dbName = System.getenv("MYSQL_DB");
        String host = System.getenv("MYSQL_HOST");
        String port = System.getenv("MYSQL_PORT");
        String dbUrl = "jdbc:mysql://"+host+":"+port + "/" +dbName;
        try {
            Class.forName(driver);
            Connection conn = (Connection) DriverManager.getConnection(dbUrl,username,password);
            Statement stmt = (Statement) conn.createStatement();
            ResultSet rs = stmt.executeQuery("show status");
            while(rs.next()){
                response.getWriter().println(rs.getString("Variable_name") + " : " +rs.getString("value"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }*/
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUsername("YOUR_ACCESSKEY"); 
        dataSource.setPassword("YOUR_SECRETKEY");

        dataSource.setUrl("jdbc:mysql://w.rdc.sae.sina.com.cn:3306/app_YOURAPPNAME"); 
        dataSource.setInitialSize(5); 
        dataSource.setMinIdle(1); 
        dataSource.setMaxActive(10); 
        dataSource.setTimeBetweenEvictionRunsMillis(10000);
        dataSource.setMinEvictableIdleTimeMillis(10000);
        try {
            Connection connection = (Connection) dataSource.getConnection();
            Statement stmt = (Statement) connection.createStatement();
            ResultSet rs = stmt.executeQuery("show status");
            while (rs.next()) {
                response.getWriter().println(rs.getString("Variable_name") + " : " + rs.getString("value"));
            }
            stmt.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        response.getWriter().append("Served at: ").append(request.getContextPath()).append("lazy");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}