Skip to content

Commit 9ad2f00

Browse files
ZwergalMcHunkyTrunk
authored andcommitted
added transaction timeout to config. updated readme.
Signed-off-by: Max Stemplinger <ms@campudus.com>
1 parent bfe810e commit 9ad2f00

File tree

5 files changed

+113
-38
lines changed

5 files changed

+113
-38
lines changed

README.md

Lines changed: 105 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This Vert.x module uses the https://github.com/mauricio/postgresql-async drivers
44

55
## Requirements
66

7-
* Vert.x 2.1+ (with Scala language module v1.0)
7+
* Vert.x 2.1+ (with Scala language module v1.0.1+)
88
* A working PostgreSQL or MySQL server
99
* For testing PostgreSQL: A 'testdb' database on a local PostgreSQL install and a user called 'vertx'
1010
* For testing MySQL: A 'testdb' database on a local MySQL install and a user called 'root'
@@ -102,10 +102,111 @@ Creates a prepared statement and lets you fill the `?` with values.
102102
{
103103
"action" : "prepared",
104104
"statement" : "SELECT * FROM some_test WHERE name=? AND money > ?",
105-
"values" : ["John", 1000]
105+
"values" : ["Mr. Test", 15]
106106
}
107+
108+
### raw - Raw commands
109+
110+
Use this action to send arbitrary commands to the database. You should be able to submit any query or insertion with this command.
111+
112+
Here is an example for creating a table in PostgreSQL:
113+
114+
{
115+
"action" : "raw",
116+
"command" : "CREATE TABLE some_test (
117+
id SERIAL,
118+
name VARCHAR(255),
119+
email VARCHAR(255),
120+
is_male BOOLEAN,
121+
age INT,
122+
money FLOAT,
123+
wedding_date DATE
124+
);"
125+
}
126+
127+
And if you want to drop it again, you can send the following:
128+
129+
{
130+
"action" : "raw",
131+
"command" : "DROP TABLE some_test;"
132+
}
133+
134+
### Transactions
135+
136+
These commands let you begin a transaction and send an arbitrary number of statements within the started transaction. You can then commit or rollback the transaction.
137+
Nested transactions are not possible until now!
138+
139+
Remember to reply to the messages after you send the `begin` command. Look in the docs how this works (e.g. for Java: [http://vertx.io/core_manual_java.html#replying-to-messages](http://vertx.io/core_manual_java.html#replying-to-messages)).
140+
With replying to the messages, the module is able to send all statements within the same transaction. If you don't reply within the `timeoutTransaction` interval, the transaction will automatically fail and rollback.
141+
142+
#### transaction begin
143+
144+
This command starts a transaction. You get an Ok message back to which you can then reply with more statements.
145+
146+
{
147+
"action" : "begin"
148+
}
149+
150+
#### transaction commit
151+
152+
To commit a transaction you have to send the `commit` command.
153+
154+
{
155+
"action" : "commit"
156+
}
157+
158+
#### transaction rollback
159+
160+
To rollback a transaction you have to send the `rollback` command.
161+
162+
{
163+
"action" : "rollback"
164+
}
165+
166+
#### Example for a transaction
167+
168+
Here is a small example on how a transaction works.
169+
170+
{
171+
"action" : "begin"
172+
}
173+
174+
This will start the transaction. You get this response:
107175

108-
### transaction
176+
{
177+
"status" : "ok"
178+
}
179+
180+
You can then reply to this message with the commands `select`, `prepared`, `insert` and `raw`.
181+
A possible reply could be this:
182+
183+
{
184+
"action" : "raw",
185+
"command" : "UPDATE some_test SET email = 'foo@bar.com' WHERE id = 1"
186+
}
187+
188+
You get a reply back depending on the statement you sent. In this case the answer would be:
189+
190+
{
191+
"status" : "ok",
192+
"rows" : 1,
193+
"message" : "UPDATE 1"
194+
}
195+
196+
If you want to make more statements you just have to reply to this message again with the next statement.
197+
When you have done all statements you can `commit` or `rollback` the transaction.
198+
199+
{
200+
"action" : "commit"
201+
}
202+
203+
If everything worked, the last answer will be:
204+
205+
{
206+
"status" : "ok"
207+
}
208+
209+
#### old transaction command (deprecated, use the new transaction mechanism with begin and commit)
109210

110211
Takes several statements and wraps them into a single transaction for the server to process. Use `statement : [...actions...]` to create such a transaction. Only `select`, `insert` and `raw` commands are allowed right now.
111212

@@ -129,33 +230,7 @@ Takes several statements and wraps them into a single transaction for the server
129230
}
130231
]
131232
}
132-
133-
### raw - Raw commands
134-
135-
Use this action to send arbitrary commands to the database. You should be able to do submit any query or insertion with this command.
136-
137-
Here is an example for creating a table in PostgreSQL:
138-
139-
{
140-
"action" : "raw",
141-
"command" : "CREATE TABLE some_test (
142-
id SERIAL,
143-
name VARCHAR(255),
144-
email VARCHAR(255),
145-
is_male BOOLEAN,
146-
age INT,
147-
money FLOAT,
148-
wedding_date DATE
149-
);"
150-
}
151-
152-
And if you want to drop it again, you can send the following:
153-
154-
{
155-
"action" : "raw",
156-
"command" : "DROP TABLE some_test;"
157-
}
158-
233+
159234
## Planned actions
160235

161236
You can always use `raw` to do anything on the database. If the statement is a query, it will return its results just like a `select`.

src/main/scala/io/vertx/asyncsql/Starter.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ class Starter extends Verticle {
2525
val dbType = getDatabaseType(config)
2626
val configuration = getConfiguration(config, dbType)
2727
val maxPoolSize = config.getInteger("maxPoolSize", 10)
28+
val transactionTimeout = config.getLong("transactionTimeout", 500L)
2829

2930
handler = dbType match {
30-
case "postgresql" => new PostgreSqlConnectionHandler(this, configuration, maxPoolSize)
31-
case "mysql" => new MySqlConnectionHandler(this, configuration, maxPoolSize)
31+
case "postgresql" => new PostgreSqlConnectionHandler(this, configuration, maxPoolSize, transactionTimeout)
32+
case "mysql" => new MySqlConnectionHandler(this, configuration, maxPoolSize, transactionTimeout)
3233
}
3334
vertx.eventBus.registerHandler(address, handler)
3435

src/main/scala/io/vertx/asyncsql/database/ConnectionHandler.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ trait ConnectionHandler extends ScalaBusMod {
2222

2323
val config: Configuration
2424
val maxPoolSize: Int
25+
val transactionTimeout: Long
2526

2627
lazy val vertx: Vertx = verticle.vertx
2728
lazy val container: Container = verticle.container
@@ -36,8 +37,6 @@ trait ConnectionHandler extends ScalaBusMod {
3637

3738
def statementDelimiter: String = ";"
3839

39-
private def timeout = 500L /* FIXME from config file! */
40-
4140
import org.vertx.scala.core.eventbus._
4241

4342
private def receiver(withConnectionFn: (Connection => Future[SyncReply]) => Future[SyncReply]): Receive = (msg: Message[JsonObject]) => {
@@ -62,7 +61,7 @@ trait ConnectionHandler extends ScalaBusMod {
6261

6362
private def mapRepliesToTransactionReceive(c: Connection): BusModReply => BusModReply = {
6463
case AsyncReply(receiveEndFuture) => AsyncReply(receiveEndFuture.map(mapRepliesToTransactionReceive(c)))
65-
case Ok(v, None) => Ok(v, Some(ReceiverWithTimeout(inTransactionReceive(c), timeout, () => failTransaction(c))))
64+
case Ok(v, None) => Ok(v, Some(ReceiverWithTimeout(inTransactionReceive(c), transactionTimeout, () => failTransaction(c))))
6665
case x => x
6766
}
6867

@@ -81,7 +80,7 @@ trait ConnectionHandler extends ScalaBusMod {
8180
protected def beginTransaction(msg: Message[JsonObject]) = AsyncReply {
8281
pool.take().flatMap { c =>
8382
c.sendQuery(transactionBegin) map { _ =>
84-
Ok(Json.obj(), Some(ReceiverWithTimeout(inTransactionReceive(c), timeout, () => failTransaction(c))))
83+
Ok(Json.obj(), Some(ReceiverWithTimeout(inTransactionReceive(c), transactionTimeout, () => failTransaction(c))))
8584
}
8685
}
8786
}

src/main/scala/io/vertx/asyncsql/database/MySqlConnectionHandler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import org.vertx.scala.platform.Verticle
44
import com.github.mauricio.async.db.Configuration
55
import io.vertx.asyncsql.Starter
66

7-
class MySqlConnectionHandler(val verticle: Starter, val config: Configuration, val maxPoolSize: Int) extends ConnectionHandler {
7+
class MySqlConnectionHandler(val verticle: Starter, val config: Configuration, val maxPoolSize: Int, val transactionTimeout: Long) extends ConnectionHandler {
88
override val dbType: String = "mysql"
99

1010
override protected def escapeField(str: String): String = "`" + str.replace("`", "\\`") + "`"

src/main/scala/io/vertx/asyncsql/database/PostgreSqlConnectionHandler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import org.vertx.scala.platform.Verticle
44
import com.github.mauricio.async.db.Configuration
55
import io.vertx.asyncsql.Starter
66

7-
class PostgreSqlConnectionHandler(val verticle: Starter, val config: Configuration, val maxPoolSize: Int) extends ConnectionHandler {
7+
class PostgreSqlConnectionHandler(val verticle: Starter, val config: Configuration, val maxPoolSize: Int, val transactionTimeout: Long) extends ConnectionHandler {
88
override val dbType: String = "postgresql"
99
}

0 commit comments

Comments
 (0)