DB连接应该是一个单子吗?[英] should a db connection be a singleton?

本文是小编为大家收集整理的关于DB连接应该是一个单子吗?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

Java创建单身人士的最佳方法是什么? DB连接是否应该是单身人士(是单身人士,它是自动的线程安全)?因为理论上的数据库无法同时访问许多用户.

推荐答案

DB连接通常不应是单身人士.

两个原因:

  1. 许多DB驱动程序不是线程安全的.使用单例意味着,如果您有很多线程,它们都将共享相同的连接. Singleton图案不会给您线程Saftey.它仅允许许多线程轻松共享一个"全局"实例.
  2. 就我个人认为,单身人士经常导致不良设计:请参阅(由其他人) http://tech.puredanger.com/2007/07/03/pattern-hate-singleton/

而不是这样做,请考虑一个数据库池.游泳池是共享的(如果需要的话,可以是单身人士).当您需要进行数据库工作时,您的代码可以做到这一点:

getConnectioFromPool();

doWork()
closeConnection() // releases back to pool

样本池库:

其他推荐答案

最好的创建单身人士(截至今天)是枚举Singleton模式( Java Enum Singleton )

我怀疑,单身人士是数据库连接的必要或任何值.您可能需要一些懒惰的创建:在首先需求和缓存时创建了连接,进一步的请求将通过缓存的实例充满信心:

public ConnectionProvider {
  private Connection conn;

  public static Connection getConnection() {
    if (conn == null || conn.isClosed()) {
      conn = magicallyCreateNewConnection();
    }
    return conn;
  }
}

(不需要线程 - 同步,如果需要)

其他推荐答案

Java中最好的方法是什么 单身人士?

遵循设计模式创建指南.即私人构造函数等

DB连接应该是单身人士 (作为单身人士,它会自动 线程安全)?

在许多情况下,创建DB连接作为单身人士可能是一个糟糕的设计选择.仅当您确定不需要DB并发时才使用它.如果您同时登录了多个用户,或者即使您的单个用户产生了许多需要访问DB的线程,那么DB连接池是一个更好的选择.您可以使用Apache或Tomcat DB连接池.这些类是在软件包

中定义的
org.apache.commons.dbcp.*;

org.apache.tomcat.dbcp.dbcp.*;

dbcp代表数据库连接池.

使用连接池的最大原因是,DB访问(DML等)的平均时间比创建连接然后关闭连接所需的时间要小得多.此外,不要忘记完成交易完成后关闭结果集,准备安排和连接变量.

因为理论上的数据库不能是 许多用户访问相同的用户 时间.

为什么不呢?在大多数情况下,DB应同时使用.您有这些DB隔离级别 - read_committing,read_uncommitting,序列化等. 序列化是您的数据库成为单个用户访问的情况.

本文地址:https://www.itbaoku.cn/post/627590.html

问题描述

What is the best way in Java to create a singleton? Should a DB connection be a singleton (being a singleton it's automatically thread-safe)? Because theoretical the DB can't be accessed by many users in the same time.

推荐答案

A DB connection should not normally be a Singleton.

Two reasons:

  1. many DB drivers are not thread safe. Using a singleton means that if you have many threads, they will all share the same connection. The singleton pattern does not give you thread saftey. It merely allows many threads to easily share a "global" instance.
  2. Personally, I think Singleton often leads to bad design: See this post (by somebody else) http://tech.puredanger.com/2007/07/03/pattern-hate-singleton/

Instead of doing this consider a database pool. The pool is shared (and could be a singleton if you wanted). When you need to do database work your code does this:

getConnectioFromPool();

doWork()
closeConnection() // releases back to pool

Sample Pool Libraries:

其他推荐答案

The best way to create a singleton (as of today) is the enum singleton pattern (Java enum singleton)

I doubt, that a Singleton is necessary or of any value for a database connection. You probably want some lazy creation: a connection is created upon first demand and cached, further requests will be fullfilled with the cached instance:

public ConnectionProvider {
  private Connection conn;

  public static Connection getConnection() {
    if (conn == null || conn.isClosed()) {
      conn = magicallyCreateNewConnection();
    }
    return conn;
  }
}

(not thread safe - synchronize, if needed)

其他推荐答案

What is the best way in Java to create a singleton?

Follow the design pattern creation guidelines. i.e private constructor, etc.

Should a DB connection be a singleton (being a singleton it's automatically thread-safe)?

creating a DB connection as a singleton might be a poor design choice in many scenarios. Use it only if you are sure that you don't need DB concurrency. If you have multiple users logged in at the same time, or even if your single user spawns many threads that need to access the DB, then a DB Connection pool is a better choice. You can use either apache or tomcat db connection pools. THese classes are defined for example in the package

org.apache.commons.dbcp.*;

org.apache.tomcat.dbcp.dbcp.*;

where dbcp stands for database connection pooling.

The biggest reason for using a connection pool is that on average the time it takes for the DB access (DML etc) is much smaller than the time it takes to create a connection and then close the connection. Additionally, don't forget to close your ResultSet, PreparedStatement and Connection variables after the transaction is done.

Because theoretical the DB can't be accessed by many users in the same time.

Why not? DB in most cases is meant to be used concurrently. You have these DB isolation levels - READ_COMMITTED, READ_UNCOMMITTED, SERIALIZED etc. SERIALIZED is the case where your DB becomes single user access.