Configuring Flux to use DBCP

VERSION 3 Published

Created on: 09-Nov-2007 08:56 by flux - Last Modified:  23-Apr-2008 12:12 by Allen Schoessler

Many users of Flux have seen to employing Tomcat as its application server for dealing with the Flux Operations Console and Business Process Dashboard. Tomcat comes bundled with a tool called DBCP for managing database connections through the use of connection pools. As you are piecing together the environment you will be running Flux in, using DBCP to hold a pool of connections to your database may seem to be a better choice opposed to establishing a new connection every time you need to access the database. This guide will walk through the steps of configuring DBCP to work with Flux.

The Basics

There are two ways of using DBCP: creating a PoolingDataSource, which acts as an actual object to use when requesting connections from the pool, or registering a PoolingDriver with the DriverManager for use as a regular JDBC driver. JDBC is well supported in Flux, so this guide will be covering making a PoolingDriver object to register. Also, for the example we will be working through, MYSQL will be installed on localhost using the default port number, and the “flux” database will hold all the Flux tables.

Writing the Pooling Driver

Before beginning, ensure that you have included the tomcat-dbcp.jar file for use by your code. The PoolingDriver object uses an underlying ObjectPool (that you will not actually see) to manage connections. An ObjectPool is created when a PoolableObjectFactory object is instantiated – this is achieved through the use of the PoolableConnectionFactory. To get that factory running there are two criteria:

  1. An empty ObjectPool object that has no database or factory connected to it. This is most easily achieved through by creating a blank GenericObjectPool.
  2. A Connection Factory from which database connections will actually be established. Since we are building a PoolingDriver the DriverManagerConnectionFactory will be delegated to achieving this. With a fully functioning ObjectPool, you will be able to register it with a blank PoolingDriver (created with a simple “new” statement) using the registerDriver(String, ObjectPool) method

To put this all together, look over the following code for our MYSQL database’s setup.

GenericObjectPool connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://localhost:3306/flux", "root", "test");
PoolableConnectionFactory poolableConnectionFactory = new
PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
PoolingDriver driver = new PoolingDriver();
driver.registerPool("flux",connectionPool);  //register the driver under the name flux

This code will result in having our driver registered with the DriverManager and usable under the JDBC address jdbc:apache:commons:dbcp:{name registered under}. For example, the MYSQL connection pool we made in this example would be available through jdbc:apache:commons:dbcp:flux and we can subsequently make Connection objects by calling the static method

DriverManager.getConnection("jdbc:apache:commons:dbcp:flux")

Configuring Flux

With your Pooling Driver registered in the DriverManager, Flux can now use the pool as a URL when specifying JDBC settings. Due to this, all that really needs to be changed in your configuration is the JDBC_URL settings, everything else will remain the same. To complete our example using MYSQL review the configuration options set below for the PoolingDriver we just made.

Configuration config = factory.makeConfiguration(); 
config.setDatabaseType(DatabaseType.MYSQL);
config.setDriver("com.mysql.jdbc.Driver");
config.setUrl("jdbc:apache:commons:dbcp:/flux");
config.setJdbcUsername("root");
config.setJdbcPassword("test");
config.setRegistryPort(1099);
config.setMaxConnections(20);
config.setConcurrencyLevel(15);
 
Average User Rating
(0 ratings)




There are no comments on this document