The SQLiteConnectionPoolDataSource is an open source implementation of a javax.sql.ConnectionPoolDataSource for SQLite. The DataSource can be used to create a JDBC Connection pool in a J2E application server (via JNDI) or a standalone application via a lightweight connection pool manager (e.g. MiniConnectionPoolManager).
The SQLiteConnectionPoolDataSource is specifically designed for Xerial's SQLiteJDBC driver.
Typically, you would create a Connection Pool for a web app server like Tomcat or Glassfish. The process varies from server to server but at a high level, all you need to do is copy the sqlite-jdbc.jar and sqlite-connection-pool.jar files somewhere where your server can see it (e.g. "glassfish/lib" for Glassfish) and create a new JDBC Connection pool using the SQLiteConnectionPoolDataSource.
Once you have created connection pool for your app server, you can use it in a servlet, jsp, etc like this:
//Find a database connection
javax.naming.InitialContext ctx = new javax.naming.InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("jdbc/SQLite");
java.sql.Connection conn = ds.getConnection();
//Do something with the connection.
//Close connection so it can be released back to the pool!
conn.close();
Of course, you don't need a full blown app server to do connection pooling. If you are building a console app, an Android app, or an Applet, you can use a standalone JDBC Connection Pool Manager like MiniConnectionPoolManager. If so, you will need to invoke the SQLiteConnectionPoolDataSource manually, like this:
//Create the ConnectionPoolDataSource
SQLiteConnectionPoolDataSource dataSource = new SQLiteConnectionPoolDataSource();
dataSource.setUrl("jdbc:sqlite:c:/temp/test.db");
//Pass in some additional config options (optional)
org.sqlite.SQLiteConfig config = new org.sqlite.SQLiteConfig();
config.enforceForeignKeys(true);
config.enableLoadExtension(true);
dataSource.setConfig(config);
You can then pass the Connection Pool DataSource to a Connection Pool Manager. In this example, we will invoke a new instance of the MiniConnectionPoolManager:
//Instantiate the Connection Pool Manager
MiniConnectionPoolManager poolMgr = new MiniConnectionPoolManager(dataSource, maxConnections);
//Remove a jdbc connection from the connection pool and do something with it
Connection connection = poolMgr.getConnection();
//Close connection so it can be released back to the pool!
connection.close();
The SQLiteConnectionPoolDataSource is based on H2's implementation of a ConnectionPoolDataSource. The H2 team has allowed us to release the code under an Apache license for compatibility with Xerial's SQLiteJDBC driver. Therefore, you have multiple licensing options when distributing this software: