State of Asynchronous Database Access for Java

It has been a while since I wrote any serious technical stuff. Previously I’ve already written a bit about asynchronous database access.

I’ve been continuing work on this, mainly on the basics, the ADBCJ driver implementation . Here’s a overview of the updates:

  • Simple connection pool: This helps to avoid the costs of creating connections. It also gives a nice control how many connections are used.
  • H2-Support: Implemented a driver for the H2 database
  • MySQL Driver Restructuring: The driver has been massively simplified, mainly in term of synchronization. This makes it easier to debug and reason about it.
  • DBFuture implementation moved to CAS operations instead of locks: Less overhead, simplifies implementation and avoids potential dead locks
  • Dropped PostgreSQL driver for now: I never maintained that driver, so I dropped it for now. If someone has interrest in a PostgreSQL driver, talk to me.
  • Countless bugfixes
H2? What? Ah, the database.

H2? What? Ah, the database.

Connection Pool

In the JDBC world connection pools are the standard. And this has it’s reasons. Connection to a database needs some initial work. With a connection pool this overhead is avoided. Plus it gives you control how many connections are used. ADBCJ has now a connection-pool too.

First you need to add the pool implementation to your project:

libraryDependencies += "org.adbcj" % "adbcj-connection-pool" % "0.5-SNAPSHOT"

After that you add the prefix pooled to the connection string. This will load the ADBCJ connection pool. For examples:

ConnectionManager plainMysql 
      = ConnectionManagerProvider.createConnectionManager(
            "adbcj:mysql://localhost/adbcjtck", "root", "");

ConnectionManager pooledMysql 
      = ConnectionManagerProvider.createConnectionManager(
            "adbcj:pooled:mysql://localhost/adbcjtck", "root", "");

The connection pool can be configured by providing properties for it. Here are the supported properties with the defaults:

Map<String,String> poolConfig = new HashMap<String, String>();
poolConfig.put("pool.maxConnections","50");
poolConfig.put("pool.maxWaitForConnection","500");

ConnectionManager pooledMysql
  	= ConnectionManagerProvider.createConnectionManager(
  		"adbcj:pooled:mysql://localhost/adbcjtck",
		"root",
		"",
		poolConfig);

H2-Support

I really like the H2 database. It’s simply to handle, embed, run and operate. I often use it for smaller projects, testing, default embedded database and so on. So no wonder, I’ve added support for it. You can add it via Maven/sbt.

First you need to add the driver to your project:

libraryDependencies += "org.adbcj" % "h2-async-driver" % "0.5-SNAPSHOT"

And then you can use the driver:

// Plain connection
ConnectionManager h2 
       = ConnectionManagerProvider.createConnectionManager(
       	  "adbcj:h2://localhost:14242/adbcjtck;MVCC=TRUE",
  	  "sa",
	  "sa");

// With connection pool, recommended
ConnectionManager h2Pooled 
       = ConnectionManagerProvider.createConnectionManager(
       	   "adbcj:pooled:h2://localhost:14242/adbcjtck;MVCC=TRUE",
	   "sa",
	   "sa");

Restructuring Synchronization of MySQL driver

The synchronization of the requests and running operations in the MySQL driver was quite complex. This caused a lot of head aches fixing race conditions and finding errors. I’ve rewritten the internal synchronization of the MySQL driver to be simpler and more understandable.
Another side effect is, that the driver now pipelines request much more aggressively. This should improve performance in some scenario drastically. The cost of this is that most requests cannot be cancelled anymore for now.

Source, etc.

The source is still on GitHub (https://github.com/gamlerhart/adbcj). Also remember that I’ve a improved Scala-API here.

The stuff is on my Maven-Repo:
https://github.com/gamlerhart/gamlor-mvn/raw/master/snapshots

Group-ID and Version for all ADBCJ projects:
GroupID: org.adbcj
Version: 0.5.0-SNAPSHOT

API:
ArtifactID: adbcj-api

Mysql:
ArtifactID: mysql-async-driver

H2:
ArtifactID: h2-async-driver

Connection-Pool:
ArtifactID: adbcj-connection-pool

That’s it for now =)

Tagged on: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *