Tuesday, April 21, 2009

Initial MySQL support and the JDBC4 wrapper interface

It is now possible to use drizzle-jdbc to execute queries against a MySQL database. The changes needed to support this were very small, the only difference (right now) in the protocol is that the number of commands has been reduced in drizzle, and the server capabilities enum has been modified to mirror the capabilities in drizzle. In the near future drizzle will have a brand new protocol though, so I made the decision to separate the main protocol classes, and share whatever can be shared outside that class.

To use drizzle-jdbc against MySQL, check this example:

try {
Class.forName("org.drizzle.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new SQLException("Could not load driver");
}
// lets hope the big O doesnt sue me for using "thin" in the connection string:
Connection connection =
DriverManager.getConnection("jdbc:mysql:thin://slaveusr@localhost:3306/test_units_jdbc");
Statement statement = connection.createStatement();
statement.execute("INSERT INTO a VALUES (1)");


JDBC4 introduced the wrapper interface, so that a user of the JDBC driver can access vendor-specific functionality in the driver. To test it, I implemented the COM_BINLOG_DUMP command against MySQL, it works like this:

if(connection.isWrapperFor(ReplicationConnection.class))) {
ReplicationConnection rc = connection.unwrap(ReplicationConnection.class);
List<RawPacket> rpList = rc.startBinlogDump(891,"mysqld-bin.000001");
for(RawPacket rp : rpList) {
for(byte b:rp.getRawBytes()) {
System.out.printf("0x%x ",b);
}
System.out.printf("\n");
}
}

Of course, this is just a proof-of-concept, it gives you a list of the raw events in the binlog on the server. This is not something really usable, but it shows how to build vendor-specific stuff and access them through the JDBC4 wrapper interface. My plan is to parse the events from the binlog and make them available on a per-request basis so we don't need to store all events from the server binlog in-memory.

No comments: