CData JDBC Driver for Magento 2019 - Online Help
Questions / Feedback?

Connection Pooling

CData JDBC Driver for Magento 2019 - Build 19.0.7354

The driver implements a standard JDBC connection pool. Set UseConnectionPooling to enable the pool. The following sections show how to configure and use them.

Working with Pooled Connections

Just as you would interact with a non-pooled connection, you use standard JDBC objects to get and close connections. But, in this case, the Connection object retrieved is a handle for the physical connection owned by the connection pool. When the connection is closed, instead of the connection being destroyed, the handle is returned to the pool, where it is available for the next connection request.

You must explicitly close the connection for it to be returned to the pool.

Creating Pooled Connections

To retrieve a Connection object from the pool, you must use an instance of MagentoDataSource and set the UseConnectionPooling connection property to true.

Configuring the Connection Pool

In addition to UseConnectionPooling, set the following connection properties to control the connection pool:

  • PoolMaxSize: Define the maximum number of connections that can be open at any given time.
  • PoolIdleTimeout: Set a limit to how long connections can remain open and idle. If this limit is exceeded, the connection is returned to the pool.
  • PoolWaitTime: Set a limit to how long new connection requests should wait for a connection to become available. If this limit is exceeded, the request throws an error. By default, connection requests wait forever for a connection to become available.

MagentoDataSource Example

To start pooling connections, instantiate the MagentoDataSource with UseConnectionPooling:

MagentoDataSource magentoDataSource = new MagentoDataSource("cdata.jdbc.magento.MagentoDriver", "jdbc:magento:UseConnectionPooling=true;Schema=magento2;User=admin;Password=admin;Url=https://mymagentohost/magento2");

JNDI Example

The following example shows how to use a connection pool with JNDI.

The following code initializes the JNDI File System Service Provider. To run the example, you need to add the fscontext.jar and providerutil.jar files to your classpath. You can download these files from the Oracle Java Archive: Under the Java SE section, select Java Platform Technologies > Java Naming and Directory Interface.

Hashtable env = new Hashtable();
env.put(Context.PROVIDER_URL, "file:///tmp");

Context ctx = new InitialContext(env); 
DataSource ds = null;
Connection conn = null;
The following code registers the MagentoDataSource with the JNDI naming service, gets an instance of the DataSource from the service, and creates pooled connections from that instance.
try {
  MagentoDataSource magentoDataSource = new MagentoDataSource("cdata.jdbc.magento.MagentoDriver", "jdbc:magento:UseConnectionPooling=true;Schema=magento2;User=admin;Password=admin;Url=https://mymagentohost/magento2");
  ctx.bind("jdbc/magento", magentoDataSource);
  ds = (DataSource) ctx.lookup("jdbc/magento");

  conn = ds.getConnection();
  Statement stat = conn.createStatement();
  boolean ret = stat.execute("SELECT 1");
  ResultSet rs=stat.getResultSet(); 
} catch(Exception ex) { } finally {
  if(conn != null) conn.close();

Closing the Connection Pool

On JRE 1.3 or higher, the connection pool itself automatically closes when the application stops running. You can manually close the connection pool by invoking the close method of the DataSource object.

Copyright (c) 2020 CData Software, Inc. - All rights reserved.
Build 19.0.7354.0