alaisi/postgres-async-driver


Asynchronous PostgreSQL Java driver

License: Apache-2.0

Language: Java


postgres-async-driver - Asynchronous PostgreSQL Java driver

Maven Central

Postgres-async-driver is a non-blocking Java driver for PostgreSQL. The driver supports connection pooling, prepared statements, transactions, all standard SQL types and custom column types.

Download

Postgres-async-driver is available on Maven Central.

<dependency>
    <groupId>com.github.alaisi.pgasync</groupId>
    <artifactId>postgres-async-driver</artifactId>
    <version>0.9</version>
</dependency>

Usage

Hello world

Querying for a set returns an rx.Observable that emits a single ResultSet.

Db db = ...;
db.querySet("select 'Hello world!' as message")
    .map(result -> result.row(0).getString("message"))
    .subscribe(System.out::println)

// => Hello world

Querying for rows returns an rx.Observable that emits 0-n Rows. The rows are emitted immediately as they are received from the server instead of waiting for the entire query to complete.

Db db = ...;
db.queryRows("select unnest('{ hello, world }'::text[] as message)")
    .map(row -> row.getString("message"))
    .subscribe(System.out::println)

// => hello
// => world

Creating a Db

Db is a connection pool that is created with com.github.pgasync.ConnectionPoolBuilder

Db db = new ConnectionPoolBuilder()
    .hostname("localhost")
    .port(5432)
    .database("db")
    .username("user")
    .password("pass")
    .poolSize(20)
    .build();

Each connection pool will start only one IO thread used in communicating with PostgreSQL backend and executing callbacks for all connections.

Prepared statements

Prepared statements use native PostgreSQL syntax $index. Supported parameter types are all primitive types, String, BigDecimal, BigInteger, UUID, temporal types in java.sql package and byte[].

db.querySet("insert into message(id, body) values($1, $2)", 123, "hello")
    .subscribe(result -> out.printf("Inserted %d rows", result.updatedRows() ));

Transactions

A transactional unit of work is started with begin(). Queries issued to the emitted Transaction are executed in the same transaction and the tx is automatically rolled back on query failure.

db.begin()
    .flatMap(tx -> tx.querySet("insert into products (name) values ($1) returning id", "saw")
        .map(productsResult -> productsResult.row(0).getLong("id"))
        .flatMap(id -> tx.querySet("insert into promotions (product_id) values ($1)", id))
        .flatMap(promotionsResult -> tx.commit())
    ).subscribe(
        __ -> System.out.println("Transaction committed"),
        Throwable::printStackTrace);

Custom data types

Support for additional data types requires registering converters to com.github.pgasync.ConnectionPoolBuilder

class JsonConverter implements Converter<example.Json> {
    @Override
    public Class<example.Json> type() {
        return example.Json.class;
    }
    @Override
    public byte[] from(example.Json json) {
        return json.toBytes();
    }
    @Override
    public example.Json to(Oid oid, byte[] value) {
        return new example.Json(new String(value, UTF_8));
    }
}

Db db = return new ConnectionPoolBuilder()
    ...
    .converters(new JsonConverter())
    .build();

Used in

References

Project Statistics

Sourcerank 8
Repository Size 298 KB
Stars 212
Forks 30
Watchers 24
Open issues 25
Dependencies 5
Contributors 8
Tags 9
Created
Last updated
Last pushed

Top Contributors See all

Antti Laisi Moe Aboulkheir Sigurd Ljødal David Sison Harald Nordgård-Hansen Mikko Tiihonen Nizamutdinov Adel Chad Retz

Packages Referencing this Repo

com.github.alaisi.pgasync:postgres-async-driver
Asynchronous PostgreSQL Java driver
Latest release 0.9 - Updated - 212 stars
com.github.alaisi.pgasync:pg-async-driver
Asynchronous PostgreSQL Java driver
Latest release 0.1 - Published - 212 stars

Recent Tags See all

postgres-async-driver-0.9 June 10, 2016
postgres-async-driver-0.8 May 03, 2016
postgres-async-driver-0.7 December 02, 2015
postgres-async-driver-0.6 June 11, 2015
postgres-async-driver-0.5 May 05, 2015
postgres-async-driver-0.4 February 13, 2015
postgres-async-driver-0.3 December 04, 2014
postgres-async-driver-0.2 September 23, 2014
pg-async-driver-0.1 February 17, 2014

Interesting Forks See all

jaceksokol/postgres-async-driver
Asynchronous PostgreSQL Java driver
Java - Apache-2.0 - Last pushed - 2 stars

Something wrong with this page? Make a suggestion

Last synced: 2016-11-24 14:30:37 UTC

Login to resync this repository