Skip to content

Commit 0e2b4f7

Browse files
authored
Merge pull request vert-x3#259 from EmadAlblueshi/rx-tx-example
Adds transaction management examples for RxJava and RxJava 2
2 parents cf39556 + 21e0c10 commit 0e2b4f7

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

rxjava-1-examples/README.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ operations via the `flatMap` operation and then subscribes to the result.
141141

142142
link:src/main/java/io/vertx/example/rxjava/database/jdbc/Client.java[RxJava jdbc client]
143143

144+
=== Jdbc Manual Transaction Handling
145+
146+
An example showing an Rxified Jdbc api to handle transactions manually then commit if all succeeded or rollback with
147+
exception propagation to the caller in case of anyone failed.
148+
149+
link:src/main/java/io/vertx/example/rxjava/database/jdbc/Transaction.java[RxJava JDBC Transaction]
150+
144151
=== Mongo example
145152

146153
An example showing the Mongo Service Rxified api, after the client connected to Mongo, it chains
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package io.vertx.example.rxjava.database.jdbc;
2+
3+
import io.vertx.core.json.JsonArray;
4+
import io.vertx.core.json.JsonObject;
5+
import io.vertx.example.util.Runner;
6+
import io.vertx.rxjava.core.AbstractVerticle;
7+
import io.vertx.rxjava.ext.jdbc.JDBCClient;
8+
import rx.Single;
9+
import rx.exceptions.CompositeException;
10+
11+
/*
12+
* @author <a href="mailto:[email protected]">Emad Alblueshi</a>
13+
*/
14+
public class Transaction extends AbstractVerticle {
15+
16+
// Convenience method so you can run it in your IDE
17+
public static void main(String[] args) {
18+
Runner.runExample(Transaction.class);
19+
}
20+
21+
@Override
22+
public void start() throws Exception {
23+
24+
JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true")
25+
.put("driver_class", "org.hsqldb.jdbcDriver");
26+
27+
String sql = "CREATE TABLE colors (" +
28+
"id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) PRIMARY KEY, " +
29+
"name VARCHAR(255), " +
30+
"datetime TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL)";
31+
32+
JDBCClient client = JDBCClient.createShared(vertx, config);
33+
34+
// Connect to the database
35+
client
36+
.rxGetConnection()
37+
.flatMap(conn ->
38+
conn
39+
// Disable auto commit to handle transaction manually
40+
.rxSetAutoCommit(false)
41+
// Create table
42+
.flatMap(autoCommit -> conn.rxExecute(sql))
43+
// Insert colors
44+
.flatMap(executed -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("BLACK")))
45+
.flatMap(updateResult -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("WHITE")))
46+
.flatMap(updateResult -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("PURPLE")))
47+
// Commit if all succeeded
48+
.flatMap(updateResult -> conn.rxCommit().map(commit -> updateResult))
49+
// Rollback if any failed with exception propagation
50+
.onErrorResumeNext(ex -> conn.rxRollback()
51+
.onErrorResumeNext(ex2 -> Single.error(new CompositeException(ex, ex2)))
52+
.flatMap(ignore -> Single.error(ex))
53+
)
54+
// Get colors if all succeeded
55+
.flatMap(updateResult -> conn.rxQuery("SELECT * FROM colors"))
56+
// Close the connection regardless succeeded or failed
57+
.doAfterTerminate(conn::close)
58+
).subscribe(resultSet -> {
59+
// Subscribe to get the final result
60+
System.out.println("Results : " + resultSet.getRows());
61+
}, Throwable::printStackTrace);
62+
}
63+
}

rxjava-2-examples/README.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ operations via the `flatMap` operation and then subscribes to the result.
142142

143143
link:src/main/java/io/vertx/example/reactivex/database/jdbc/Client.java[RxJava 2 JDBC client]
144144

145+
=== Jdbc Manual Transaction Handling
146+
147+
An example showing an Rxified Jdbc api to handle transactions manually then commit if all succeeded or rollback with
148+
exception propagation to the caller in case of anyone failed.
149+
150+
link:src/main/java/io/vertx/example/reactivex/database/jdbc/Transaction.java[RxJava Jdbc Transaction]
151+
145152
=== Mongo example
146153

147154
An example showing the Mongo Service Rxified api, after the client connected to Mongo, it chains
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package io.vertx.example.reactivex.database.jdbc;
2+
3+
import io.reactivex.Single;
4+
import io.reactivex.exceptions.CompositeException;
5+
import io.vertx.core.json.JsonArray;
6+
import io.vertx.core.json.JsonObject;
7+
import io.vertx.example.util.Runner;
8+
import io.vertx.reactivex.core.AbstractVerticle;
9+
import io.vertx.reactivex.ext.jdbc.JDBCClient;
10+
11+
/*
12+
* @author <a href="mailto:[email protected]">Emad Alblueshi</a>
13+
*/
14+
public class Transaction extends AbstractVerticle {
15+
16+
// Convenience method so you can run it in your IDE
17+
public static void main(String[] args) {
18+
Runner.runExample(Transaction.class);
19+
}
20+
21+
@Override
22+
public void start() throws Exception {
23+
24+
JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true")
25+
.put("driver_class", "org.hsqldb.jdbcDriver");
26+
27+
String sql = "CREATE TABLE colors (" +
28+
"id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) PRIMARY KEY, " +
29+
"name VARCHAR(255), " +
30+
"datetime TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL)";
31+
32+
JDBCClient client = JDBCClient.createShared(vertx, config);
33+
34+
// Connect to the database
35+
client
36+
.rxGetConnection()
37+
.flatMap(conn ->
38+
conn
39+
// Disable auto commit to handle transaction manually
40+
.rxSetAutoCommit(false)
41+
// Switch from Completable to default Single value
42+
.toSingleDefault(false)
43+
// Create table
44+
.flatMap(autoCommit -> conn.rxExecute(sql).toSingleDefault(true))
45+
// Insert colors
46+
.flatMap(executed -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("BLACK")))
47+
.flatMap(updateResult -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("WHITE")))
48+
.flatMap(updateResult -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("PURPLE")))
49+
// commit if all succeeded
50+
.flatMap(updateResult -> conn.rxCommit().toSingleDefault(true).map(commit -> updateResult))
51+
// Rollback if any failed with exception propagation
52+
.onErrorResumeNext(ex -> conn.rxRollback()
53+
.toSingleDefault(true)
54+
.onErrorResumeNext(ex2 -> Single.error(new CompositeException(ex, ex2)))
55+
.flatMap(ignore -> Single.error(ex))
56+
)
57+
// Get colors if all succeeded
58+
.flatMap(updateResult -> conn.rxQuery("SELECT * FROM colors"))
59+
// close the connection regardless succeeded or failed
60+
.doAfterTerminate(conn::close)
61+
).subscribe(resultSet -> {
62+
// Subscribe to get the final result
63+
System.out.println("Results : " + resultSet.getRows());
64+
}, Throwable::printStackTrace);
65+
}
66+
}

0 commit comments

Comments
 (0)