简体   繁体   English

可以使用Singleton模式与Mysql jdbc连接吗?

[英]can be used Singleton pattern with Mysql jdbc connection?

this is the code that i used to establish jdbc connection. 这是我用来建立jdbc连接的代码。 can i use getInstance() shown in below. 我可以使用下面显示的getInstance() MysqlConnection class object can be called in other classes using MysqlConnection.getInstance() . 可以使用MysqlConnection.getInstance()在其他类中调用MysqlConnection类对象。 this code segment is working properly.but i want to know, is this code incorrect? 这个代码段工作正常。但是我想知道,这个代码不正确吗? can i use like this? 我可以这样使用吗?

public class MysqlConnection {

//use singleton design patern 
private static MysqlConnection instance;
public static MysqlConnection getInstance(){
  if(instance == null) {
    instance = new MysqlConnection();
  }
  return instance;
}

// init database constants
private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/storetest";
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
private static final String MAX_POOL = "250";

// init connection object
private Connection connection;
// init properties object
private Properties properties;

// create properties
private Properties getProperties() {
  if(properties == null) {
    properties = new Properties();
    properties.setProperty("user", USERNAME);
    properties.setProperty("password", PASSWORD);
    properties.setProperty("MaxPooledStatements", MAX_POOL);
  }
  return properties;
}

// connect database
public Connection connect() {
    if (connection == null) {
        try {
            Class.forName(DATABASE_DRIVER);
            connection = (Connection) DriverManager.getConnection(DATABASE_URL, getProperties());
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
    return connection;
}

// disconnect database
public void disconnect() {
  if(connection != null) {
    try {
      connection.close();
      connection = null;
    } catch (SQLException e) {
        e.printStackTrace();
    }
  }
}    

}

Using a Singleton for a SqlConnection object is not a good idea. SqlConnection对象使用Singleton不是一个好主意。
See getting db connection through singleton class 查看通过单例类获取数据库连接

But if you still want to make it singleton, replace: 但是,如果仍然要使其单例,请替换:

public class MysqlConnection {

    //use singleton design patern 
    private static MysqlConnection instance;
    public static MysqlConnection getInstance(){
        if(instance == null){
            instance = new MysqlConnection();
        }
        return instance;
    }

With enum : 枚举

public enum MysqlConnection {
    INSTANCE;
    public static MysqlConnection getInstance(){
        return INSTANCE;
    }

For more details see Implementing Singleton as Enum in Java 有关更多详细信息,请参见在Java中将Singleton实现为枚举。

You should use dependency injection to inject your dependencies in the classes that need them. 您应该使用依赖项注入将依赖项注入需要它们的类中。

An example can be found here : http://www.journaldev.com/2394/dependency-injection-design-pattern-in-java-example-tutorial 可以在这里找到一个示例: http : //www.journaldev.com/2394/dependency-injection-design-pattern-in-java-example-tutorial

A singleton is just another global and is an antipattern in the way that you are using it here. 单例只是另一个全局变量,是您在此处使用它的方式的反模式。 What is so bad about singletons? 单身人士有什么不好呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM