简体   繁体   English

DataSource如何成为对ConnectionPoolDataSource的引用。

[英]How can DataSource be a reference to a ConnectionPoolDataSource.

The preferred way of obtaining a connection is through a DataSource from what I have read. 获取连接的首选方法是通过我阅读的数据源。 There are different interfaces such as DataSource and ConnectionPoolDataSource . 有不同的接口,例如DataSourceConnectionPoolDataSource Let say we use PostgreSQL driver and want to use connection pooling on a Glassfish server. 假设我们使用PostgreSQL驱动程序,并且想在Glassfish服务器上使用连接池。

In your application code you invoke getConnection() on a property of type DataSource . 在您的应用程序代码中,您对DataSource类型的属性调用getConnection() How is this possible? 这怎么可能? Haven't Glassfish created a datasource of type ConnectionPoolDataSource (or more correctly an implementing class) and bound it to a JNDI name and when you get a datasource by using the JNDI name you get an object of ConnectionPoolDataSource not DataSource ?? Glassfish尚未创建类型为ConnectionPoolDataSource的数据源(或更正确地说是实现类),并将其绑定到JNDI名称,当您使用JNDI名称获得数据源时,您将获得ConnectionPoolDataSource对象而不是DataSource对象? ConnectionPoolDataSource does not have a getConnection() method. ConnectionPoolDataSource没有getConnection()方法。 I don't understand this server magic. 我不明白这种服务器魔术。

Could someone explain how all this fits together? 有人可以解释一下这一切如何融合吗?

The DataSource , Driver or ConnectionPoolDataSource that you can select in the Glassfish config is not exposed to your application directly, instead the Application Server has its own DataSource that maintains a connection pool, this datasource uses the configured DataSource , Driver or ConnectionPoolDataSource as the factory for the connections it will keep in its pool. 您可以在Glassfish配置中选择的DataSourceDriverConnectionPoolDataSource不会直接公开给您的应用程序,而是Application Server有自己的DataSource来维护连接池,此数据源将配置的DataSourceDriverConnectionPoolDataSource用作工厂。连接将保留在其池中。

So when you configure Glassfish with a ConnectionPoolDataSource , it uses the ConnectionPoolDataSource to create the physical connections ( PooledConnection objects) for a connection pool. 因此,当您使用ConnectionPoolDataSource配置Glassfish时,它将使用ConnectionPoolDataSource为连接池创建物理连接( PooledConnection对象)。 This connection pool is kept by the application server DataSource implementation. 此连接池由应用程序服务器的DataSource实现保留。 Your application then access that connection pool using this DataSource . 然后,您的应用程序使用此DataSource访问该连接池。 The DataSource hands out logical Connection objects from the connection pool. 数据源从连接池中分发逻辑Connection对象。

The exact inner workings of a logical and physical connection are implementation dependent, but these logical connections are usually some kind of proxy or wrapper around the physical connection. 逻辑和物理连接的确切内部工作方式取决于实现,但是这些逻辑连接通常是围绕物理连接的某种代理或包装。 When you obtain the logical connection, the physical connection is checked out from the connection pool. 获得逻辑连接后,将从连接池中检出物理连接。 When you close the logical connection, the connection pool receives a signal that the physical connection is available again and returns it to the connection pool. 当您关闭逻辑连接时,连接池会收到一个信号,表明物理连接再次可用,并将其返回到连接池。

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

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