getConnection 是否总是打开一个新连接?

Does getConnection always open a new connection?

我在 Java 文件中找到了这个方法,我想知道这里发生了什么?这真的有效吗?我认为这个方法什么都不做,因为 getConnection() 方法创建了一个新连接,然后它被关闭了。我说得对吗?

public void closeAllConnections()
    {
        for(String db : this.dbList)
            try {
                DataSource ds = (DataSource) this.applicationContext.getBean(db+"DataSource"); 
                ds.getConnection().close();
            } catch (Exception e) {
                //...
            }
    }

这取决于您的数据源是如何定义的。

如果它使用连接池,它可以重用现有连接

例如见oracle解释

Connection pooling in the JDBC 2.0 extension API is a framework for caching database connections. This allows reuse of physical connections and reduced overhead for your application.