Skip to content
This repository has been archived by the owner on Jun 2, 2021. It is now read-only.

Latest commit

 

History

History
73 lines (51 loc) · 1.66 KB

README.md

File metadata and controls

73 lines (51 loc) · 1.66 KB

MySQL DB Patterns

by Andrew Brampton 2013

Intro

Some useful java code backed by JDBC that implements some common patterns.

So far a Condition object, and a Queue are implemented.

	<dependency>
		<groupId>net.bramp.db-patterns</groupId>
		<artifactId>db-patterns</artifactId>
		<version>0.1.1</version>
	</dependency>

Condition

A distributed Java Condition

  DataSource ds = ...
  Condition condition = new MySQLSleepBasedCondition(ds, "lockname");
  condition.await(); // Blocks until a notify
  
  // on another thread (or process, or machine)
  condition.notify();

The MySQLSleepBasedCondition is based on the MySQL SLEEP() and KILL QUERY

The thread that is woken up is guaranteed to be the one that has waited the longest.

Queue

A distributed MySQL backed Java BlockingQueue

  DataSource ds = ...
  BlockingQueue<String> queue = new MySQLBasedQueue<String>(ds, "queue name", String.class);
  queue.add("Some String");
  
  // on another thread (or process, or machine)
  String s = queue.poll(); // Non blocking
  // or
  String s = queue.take(); // Blocks until element available

The MySQLBasedQueue uses the MySQLSleepBasedCondition to help form a blocking queue, that can work without polling the database for new work.

Build and Release

To build this project use mvn.

To push a release to maven central use the standard maven release plugin, and Sonatype's OSS repo:

mvn release:prepare
mvn release:perform

Useful Articles

https://blog.engineyard.com/2011/5-subtle-ways-youre-using-mysql-as-a-queue-and-why-itll-bite-you