alaisi / postgres-async-driver

Asynchronous PostgreSQL Java driver
Apache License 2.0
289 stars 38 forks source link

Cannot add more async ops than pool size #30

Closed julianrz closed 7 years ago

julianrz commented 8 years ago

Expected async requests to be queued when there are no available connection in the pool, but apparently callback is not invoked when there are too many initiated transactions. it is invoked 20 times, which is pool size - but no more. No errors printed either

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

    AtomicLong ct = new AtomicLong(10000L);

    for (long n = 0; n< 10000L; n++) {
            db.begin((Transaction tx) -> tx.querySet("INSERT INTO sample (x) VALUES ($1)", 1)
                    .subscribe(
                            result -> ct.decrementAndGet()),
                    (Throwable th) -> th.printStackTrace());
    }
    while(ct.get()>0)
        Thread.sleep(1000); //this never completes
alaisi commented 8 years ago

Hi,

you are starting transactions, but never call commit()/rollback(), so the connections are not returned to the pool.

        AtomicLong ct = new AtomicLong(10000L);

        for (long n = 0; n< 10000L; n++) {
            db.begin()
              .flatMap(tx -> tx.querySet("INSERT INTO sample (x) VALUES ($1)", 1)
                      .flatMap(result -> tx.commit())
              ).subscribe(result -> ct.decrementAndGet(), Throwable::printStackTrace);
        }
        while(ct.get()>0)
            Thread.sleep(1000);