创建数据库连接的最佳方法 [英] Best way for creating database connections

查看:146
本文介绍了创建数据库连接的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java应用程序中创建数据库连接的最佳方法是什么?使用Singleton,静态方法,服务定位器,连接池还是别的什么?

What is the best way to create database connections in a Java application? Using Singleton, Static Method, Service Locator, Connection Pool or something else?

请您告诉我上述每种方法的优缺点?

Please would you let me know the pros and cons of each of the above approaches?

推荐答案

如果您的应用程序适用于多个连接,则首选使用连接池。它已经在Java中实现,您可以轻松使用它。
这个在使用tomcat的web应用程序中使用池的例子(如果你编写web-app你也可以使用tomcat的池,它会更好)

If your app will works with many connections it's preferred to use pool of connections. It's already realized in Java and you may easily use it. This example of using pool in web app that uses tomcat(if you writing web-app you also can use tomcat's pool, and it would be better)

package usepool;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;

/**
*
* @author brainless
*/
public class ConnectionPool {

private static DataSource datasource;
public static String dbURL = "jdbc:mysql://localhost:3306/"
                + "<YourDataBase>?useUnicode=true&useEncoding=true&characterEncoding=UTF-8";
public static String driverClass = "com.mysql.jdbc.Driver";
public static String userName = "root";
public static String password = "password";
public static boolean jmx = true;
public static boolean testIdle = false;
public static boolean testBorrow = true;
public static boolean testReturn = false;
public static int validationInterval = 30000;
public static int timeBetweenEviction = 30000;
public static int maxActive = 100;
public static int initialSize = 10;
public static int maxWait = 10000;
public static int removeAbandonedTimeout = 60;
public static int minEvictableIdle = 30000;
public static int minIdle = 10;
public static boolean logAbandoned = true;
public static boolean removeAbandoned = true;
public static String jdbcInterceptors = "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
        + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer";

private ConnectionPool() {
}

public static synchronized DataSource getInstance() {
    if (datasource == null) {
        PoolProperties p = new PoolProperties();
        p.setUrl(dbURL);
        p.setDriverClassName(driverClass);
        p.setUsername(userName);
        p.setPassword(password);
        p.setJmxEnabled(jmx);
        p.setTestWhileIdle(testIdle);
        p.setTestOnBorrow(testBorrow);
        p.setTestOnReturn(testReturn);
        p.setValidationInterval(validationInterval);
        p.setTimeBetweenEvictionRunsMillis(timeBetweenEviction);
        p.setMaxActive(maxActive);
        p.setInitialSize(initialSize);
        p.setMaxWait(maxWait);
        p.setRemoveAbandonedTimeout(removeAbandonedTimeout);
        p.setMinEvictableIdleTimeMillis(minEvictableIdle);
        p.setMinIdle(minIdle);
        p.setLogAbandoned(logAbandoned);
        p.setRemoveAbandoned(removeAbandoned);
        p.setJdbcInterceptors(jdbcInterceptors);
        datasource = new DataSource();
        datasource.setPoolProperties(p);
    }
    return datasource;
}

public static synchronized void closePool() {
    if (datasource != null) {
        datasource.close();
    }
}

}

这个类是单身。要在代码中建立连接,您需要使用类似的东西

This class is singleton. To get connection in your code you need to use something like this

import usepool.ConnectionPool;
/* 
* code
*/
connect = ConnectionPool.getInstance().getConnection();

这篇关于创建数据库连接的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆