我应该使用 ThreadLocal 来存储连接吗?

发布于 2025-01-18 02:34:42 字数 1729 浏览 0 评论 0原文

我对我的测试项目进行编码,并禁止在那里使用春季和冬眠。

我想从服务层管理交易。 为此,我创建了一个从池中获得连接并将其放入线路local的类。

这是字段和方法的一个示例。

  private static ThreadLocal<Connection> threadLocalConnection;
  private ComboPooledDataSource comboPooledDataSource ;

  public boolean createConnectionIfAbsent() {

    boolean isConnectionCreated = false;

    try {
      Connection currentConnection = threadLocalConnection.get();
      if(currentConnection == null) {
        Connection conn = this.comboPooledDataSource.getConnection();
        conn.setAutoCommit(false);
        threadLocalConnection.set(conn);
        isConnectionCreated = true;
      }

    } catch (SQLException e) {
      e.printStackTrace();
    }
    return isConnectionCreated;
  }

该类还具有关闭的回滚方法。

这是我如何在服务层中管理连接的示例。

public BigDecimal getTotalOrdersCount() {
    boolean connectionCreated = DBManager.getInstance().createConnectionIfAbsent();

    BigDecimal ordersCount = BigDecimal.ZERO;
    try {
        ordersCount = orderDao.getRowNumber();
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    } finally {
        if (connectionCreated) DBManager.getInstance().closeConnection();
    }
    return ordersCount;
}

Dao只是使用它来获得连接。

Connection connection = DBManager.getInstance().getConnection();

我没有找到其他方法来管理服务层的Servlet项目中的连接,您能告诉您是否还可以吗?如果没有 - 它有哪些缺点,我应该使用什么。

upd:

请注意此服务方法。让我们假设DAO中的每种方法都从池中获取连接并关闭它。 我确实知道我需要Connection.setautocommit(false);要开始交易,但是在这种情况下该怎么办? 当单个方法调用2 dao时。 只是放弃交易处理?

void setStatusDeclinedAndRefund() {
// sets Order status to DECLINED
// refund money to user's balance
}

I code my Test project and it is prohibited to use Spring and Hibernate there.

I wanted to manage my transactions from Service layer.
For this I have created a class that gets a Connection from the pool and puts it in the ThreadLocal.

This is an example of the fields and the method.

  private static ThreadLocal<Connection> threadLocalConnection;
  private ComboPooledDataSource comboPooledDataSource ;

  public boolean createConnectionIfAbsent() {

    boolean isConnectionCreated = false;

    try {
      Connection currentConnection = threadLocalConnection.get();
      if(currentConnection == null) {
        Connection conn = this.comboPooledDataSource.getConnection();
        conn.setAutoCommit(false);
        threadLocalConnection.set(conn);
        isConnectionCreated = true;
      }

    } catch (SQLException e) {
      e.printStackTrace();
    }
    return isConnectionCreated;
  }

The class has also close, rollback methods.

Here is the example of how I manage Connections in a Service Layer.

public BigDecimal getTotalOrdersCount() {
    boolean connectionCreated = DBManager.getInstance().createConnectionIfAbsent();

    BigDecimal ordersCount = BigDecimal.ZERO;
    try {
        ordersCount = orderDao.getRowNumber();
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    } finally {
        if (connectionCreated) DBManager.getInstance().closeConnection();
    }
    return ordersCount;
}

Dao just uses this to get the connection.

Connection connection = DBManager.getInstance().getConnection();

I found no other way to manage connections in a Servlet project from a Service layer, could you please tell if it is ok? If not - what drawbacks does it have and what should I use instead.

UPD:

Please pay attention to this Service method. Let's assume that Each method in DAO gets the Connection from a pool and closes it.
I do know that I need connection.setAutoCommit(false); to start a transaction, but what to do it in this kind of a situation?
When a single methods calls 2 DAO.
Just give up on a transaction handling?

void setStatusDeclinedAndRefund() {
// sets Order status to DECLINED
// refund money to user's balance
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北座城市 2025-01-25 02:34:42

否。

不要第二猜连接池。以标准方式使用它:建立连接,使用它,将其关闭。

对于给定线程中的每个数据库交互,无需使用相同的连接。另外,如果您将每个线程分配一个连接,您将遇到严重的生命问题,因为通常,请求处理线程比池中的连接更多。

No.

Don't second guess the connection pool. Use it in the standard way: get a connection, use it, close it.

There is no need to use the same connection for every database interaction in a given thread. Also, you'll have serious liveliness problems if you allocate each thread a connection, because typically there are way more request processing threads than there are connections in the pool.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文