/jasync-sql

Java async database driver for MySQL and PostgreSQL written in Kotlin

Primary LanguageKotlinApache License 2.0Apache-2.0

jasync-sql

Chat at https://gitter.im/jasync-sql/support Download Build Status Apache License V.2 codecov Awesome Kotlin Badge

jasync-sql is a Simple, Netty based, asynchronous, performant and reliable database drivers for PostgreSQL and MySQL written in Kotlin.

Show your ❤ with a ★

Getting started

// Connection to MySQL DB
Connection connection = new MySQLConnection(
      new Configuration(
        "username",
        "host.com",
        3306,
        "password",
        "schema"
      )
    );
// Connection to PostgreSQL DB    
Connection connection = new PostgreSQLConnection(
      new Configuration(
        "username",
        "host.com",
        5324,
        "password",
        "schema"
      )
    );
// And then connect    
CompletableFuture<Connection> connectFuture = connection.connect()
// Wait for connection to be ready   
// ...    
// Execute query
CompletableFuture<QueryResult> future = connection.sendPreparedStatement("select * from table");
// Close the connection
connection.disconnect().get()

The above example is a simple usage of the driver. In most real world scenarios there is one missing part here. Each Connection from above is capable of handling one query at a time. In order to be able to execute multiple connections simultanously one should use a ConnectionPool.

ConnectionPool is responsible to manage connections, borrow them for query execution and validate they are still alive. Aside from construction, ConnectionPool has the same interface as a Connection. Here is how a connection pool is constructed:

PoolConfiguration poolConfiguration = new PoolConfiguration(
        100,                            // maxObjects
        TimeUnit.MINUTES.toMillis(15),  // maxIdle
        10_000,                         // maxQueueSize
        TimeUnit.SECONDS.toMillis(30)   // validationInterval
);
Connection connectionPool = new ConnectionPool<>(
        // for PostgreSQL use PostgreSQLConnectionFactory instead of MySQLConnectionFactory
                                  new MySQLConnectionFactory(configuration), poolConfiguration);

See a full example at jasync-mysql-example and jasync-postgresql-example.
More samples on the samples dir.

For docs and info see the wiki.

Download

  • Note: The regular artifact is netty 4.1. Netty 4.0 version looks like this 0.8.41-netty4.0 etc'.

Maven

<!-- mysql -->
<dependency>
  <groupId>com.github.jasync-sql</groupId>
  <artifactId>jasync-mysql</artifactId>
  <version>0.8.41</version>
</dependency>
<!-- postgresql -->
<dependency>
  <groupId>com.github.jasync-sql</groupId>
  <artifactId>jasync-postgresql</artifactId>
  <version>0.8.41</version>
</dependency>
<!-- add jcenter repo: -->
<repositories>
  <repository>
    <id>jcenter</id>
    <url>https://jcenter.bintray.com/</url>
  </repository>
</repositories>

Gradle

dependencies {
  // mysql
  compile 'com.github.jasync-sql:jasync-mysql:0.8.41'
  // postgresql
  compile 'com.github.jasync-sql:jasync-postgresql:0.8.41'
}
// add jcenter repo:
repositories {
    jcenter()
}

Overview

This project is a port of mauricio/postgresql-async to Kotlin.
Why? Because the original lib is not maintained anymore, We use it in ob1k, and would like to remove the Scala dependency in ob1k.

This project always returns JodaTime when dealing with date types and not the java.util.Date class. (We plan to move to jdk-8 dates).

If you want information specific to the drivers, check the PostgreSQL README and the MySQL README.

You can view the project's CHANGELOG here.

Follow us on twitter: @jasyncs.

Who is using it

Add your name here!

Support

More links

Contributing

Pull requests are welcome!
See CONTRIBUTING.