Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction management for SQLClient #141

Closed
tsegismont opened this issue May 17, 2018 · 7 comments · Fixed by #143
Closed

Transaction management for SQLClient #141

tsegismont opened this issue May 17, 2018 · 7 comments · Fixed by #143
Assignees
Milestone

Comments

@tsegismont
Copy link
Contributor

tsegismont commented May 17, 2018

When using an Rxified SQLClient, it's not obvious for everyone how to properly manage a connection. Proper transaction management is even worse.

It would help to have utility methods to manage a connection, a transaction, or both.

Let's take an example:

public class JDBCHelper {

  static <T> Flowable<T> withConnectionFlowable(SQLClient sqlClient, Function<SQLConnection, Flowable<T>> flowable) {
    return sqlClient.rxGetConnection().flatMapPublisher(sqlConnection -> {
      return flowable.apply(sqlConnection).doFinally(sqlConnection::close);
    });
  }

  static <T> Flowable<T> inTransactionFlowable(SQLClient sqlClient, Function<SQLConnection, Flowable<T>> flowable) {
    return withConnectionFlowable(sqlClient, flowable);
  }

  static <T> Flowable<T> inTransactionFlowable(SQLConnection sqlConnection, Function<SQLConnection, Flowable<T>> flowable) {
    return sqlConnection.rxSetAutoCommit(false).<T>toFlowable()
      .concatWith(flowable.apply(sqlConnection))
      .concatWith(sqlConnection.rxCommit().toFlowable())
      .onErrorResumeNext(throwable -> {
        return sqlConnection.rxRollback().onErrorComplete().andThen(Flowable.error(throwable));
      }).concatWith(sqlConnection.rxSetAutoCommit(true).toFlowable());
  }
}

Now it's easy to write such kind of flow:

    JDBCHelper.withConnectionFlowable(client, conn -> {
      Flowable<JsonArray> setupDb = JDBCHelper.inTransactionFlowable(conn, sqlConnection -> {
        return conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("BLACK")).toCompletable().toFlowable()
          .concatWith(conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("WHITE")).toCompletable().toFlowable())
          .concatWith(conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("PURPLE")).toCompletable().toFlowable());
      }).ignoreElements().toFlowable();
      return setupDb
        .concatWith(conn.rxQuery("SELECT * FROM colors").flatMapPublisher(resultSet -> Flowable.fromIterable(resultSet.getResults())));
    }).subscribe(System.out::println, Throwable::printStackTrace);

Of course, we'd need other helper methods for Single, Completable and Maybe.

Also, it's not clear to me where we'd add this code. In this repo? In vertx-sql-common? Should we use utility classes as in the example above? Or should we add these methods to Rxified SQLClient classes? In this case, how?

@vietj
Copy link
Contributor

vietj commented May 17, 2018

the appropriate location in an helper class in vertx-rx-java2/src/main/java/io/vertx/reactivex/ext/sql

@tsegismont
Copy link
Contributor Author

Thanks @vietj ! Any comment on the proposal itself?

@vietj
Copy link
Contributor

vietj commented May 18, 2018

it sounds a better alternative to my initial proposal I did for 3.6 to ease finally like semantics on Flowable. So I'm happy you made it.

I think that withConnectionFlowable should rather be in the normal RxHelper using instead the Closeable interface since SqlConnection implements this interface (with a better name).

@tsegismont
Copy link
Contributor Author

So I'm happy you made it.

Well, just threw some code here so far, not tested yet :)

I think that withConnectionFlowable should rather be in the normal RxHelper using instead the Closeable interface since SqlConnection implements this interface (with a better name).

👍

@tsegismont tsegismont changed the title Helper methods for SQL access Transaction management for SQLClient May 24, 2018
@tsegismont tsegismont added this to the 3.6.0 milestone May 24, 2018
@tsegismont
Copy link
Contributor Author

tsegismont commented May 24, 2018

@vietj as discussed off-list, narrowing down the scope of this issue to transaction management only. We will address Closeable / AutoCloseable separately. The PR will have quite a lot of changes anyway :)

@vietj
Copy link
Contributor

vietj commented Jun 8, 2018

I guess we can close this PR ?

@tsegismont
Copy link
Contributor Author

tsegismont commented Jun 8, 2018

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants