如何避免java中重复的数据库连接?

时间:2017-04-02 04:30:42

标签: java mysql sql jdbc

我有2节课。 C_DB和C_Test。

在C_DB中,getConnection()方法返回一个连接。如果我多次调用getConnection(),它将重新连接到数据库并返回多个连接。

有没有办法可以避免这种情况?我想检查连接是否存在,然后只返回该连接而不是创建一个新连接。因此,只有一个与db的连接。

C_DB

public class C_DB {
    Connection con;

    public C_DB() {
        String dbLink = "jdbc:mysql://localhost:3306/database";
        String dbUser = "root";
        String dbPass = "";

        try {
            con = DriverManager.getConnection(dbLink, dbUser, dbPass);
        } catch (SQLException e) {
            throw new IllegalStateException("DB Errors: ", e);
        }
    }

    public Connection getConnection() {
        return con;
    }
}

C_Test

public class C_Test {

    public static void main(String[] args) throws Exception {
        Connection con1 = new C_DB().getConnection(); // new connection
        Connection con2 = new C_DB().getConnection(); // new duplicate connection
        Connection con3 = new C_DB().getConnection(); // new duplicate connection
    }

}

2 个答案:

答案 0 :(得分:2)

您可以使用连接池并将其最大池大小设置为1,因为@Elliott在评论中指出,或者您可以实现类似的内容。

public class C_DB {

    private static C_DB instance;

    private Connection con;

    private C_DB() {
        String dbLink = "jdbc:mysql://localhost:3306/database";
        String dbUser = "root";
        String dbPass = "";
        try {
            con = DriverManager.getConnection(dbLink, dbUser, dbPass);
        } catch (SQLException e) {
            throw new IllegalStateException("DB Errors: ", e);
        }
    }

    public static Connection getConnection(){
        if(instance == null){
           instance = new C_DB();
        }
        return instance.con;
    }

}

答案 1 :(得分:0)

您应该使用 { "_id" : ObjectId("58d6cbc14124691cd8154d72"), "correlativeCode" : "CSLLPA53E20M017W", "registrationMethod" : "taken", "originPlace" : "INSPI", "temperature" : 16, "sampleStatus" : [ { "nameStatus" : "status1", "place" : "place1", "rejectionReason" : "Nothing", "user" : "user1", "_id" : ObjectId("58d6cbc14124691cd8154d73") }, { "nameStatus" : "status2", "place" : "place2", "rejectionReason" : "Nothing", "user" : "user4", "_id" : ObjectId("58d6cbc14124691cd8154d73") }, { "nameStatus" : "status3", "place" : "place3", "rejectionReason" : "Nothing", "user" : "user3", "_id" : ObjectId("58d6cbc14124691cd8154d73") }, { "nameStatus" : "status4", "place" : "place4", "rejectionReason" : "Nothing", "user" : "user1", "_id" : ObjectId("58d6cbc14124691cd8154d73") }, { "nameStatus" : "status5", "place" : "place5", "rejectionReason" : "Nothing", "user" : "user5", "_id" : ObjectId("58d6cbc14124691cd8154d73") } ] } 设计模式来避免多个实例。

类似的东西:

Singleton

获取连接:

public class C_DB {

    private static final Connection con;
    private static final C_DB singleInstance;

    private C_DB() {
        String dbLink = "jdbc:mysql://localhost:3306/database";
        String dbUser = "root";
        String dbPass = "";

        try {
            con = DriverManager.getConnection(dbLink, dbUser, dbPass);
        } catch (SQLException e) {
            throw new IllegalStateException("DB Errors: ", e);
        }
    }

    public static C_DB getInstance() {
        if (singleInstance == null) {
            synchronized (C_DB.class) {
                if (singleInstance == null) {
                    singleInstance = new C_DB();
                }
            }
        }

        return singleInstance;
    }

    public Connection getConnection() {
        return con;
    }
}