{"action":"created","issue":{"url":"https://api.github.com/repos/reactiverse/reactive-pg-client/issues/112","repository_url":"https://api.github.com/repos/reactiverse/reactive-pg-client","labels_url":"https://api.github.com/repos/reactiverse/reactive-pg-client/issues/112/labels{/name}","comments_url":"https://api.github.com/repos/reactiverse/reactive-pg-client/issues/112/comments","events_url":"https://api.github.com/repos/reactiverse/reactive-pg-client/issues/112/events","html_url":"https://github.com/reactiverse/reactive-pg-client/issues/112","id":320106522,"node_id":"MDU6SXNzdWUzMjAxMDY1MjI=","number":112,"title":"Discuss: Make transaction API asynchronous","user":{"login":"Narigo","id":1767865,"node_id":"MDQ6VXNlcjE3Njc4NjU=","avatar_url":"https://avatars3.githubusercontent.com/u/1767865?v=4","gravatar_id":"","url":"https://api.github.com/users/Narigo","html_url":"https://github.com/Narigo","followers_url":"https://api.github.com/users/Narigo/followers","following_url":"https://api.github.com/users/Narigo/following{/other_user}","gists_url":"https://api.github.com/users/Narigo/gists{/gist_id}","starred_url":"https://api.github.com/users/Narigo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Narigo/subscriptions","organizations_url":"https://api.github.com/users/Narigo/orgs","repos_url":"https://api.github.com/users/Narigo/repos","events_url":"https://api.github.com/users/Narigo/events{/privacy}","received_events_url":"https://api.github.com/users/Narigo/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":8,"created_at":"2018-05-03T22:52:59Z","updated_at":"2018-07-05T15:32:09Z","closed_at":null,"author_association":"NONE","body":"The current handling of transactions look like this:\r\n\r\n```java\r\n    // Begin the transaction\r\n    PgTransaction tx = conn.begin();\r\n\r\n    // Various statements\r\n    conn.query(\"INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')\", ar -> {});\r\n    conn.query(\"INSERT INTO Users (first_name,last_name) VALUES ('Emad','Alblueshi')\", ar -> {});\r\n\r\n    // Commit the transaction\r\n    tx.commit(ar -> {\r\n      if (ar.succeeded()) {\r\n        System.out.println(\"Transaction succeeded\");\r\n      } else {\r\n        System.out.println(\"Transaction failed \" + ar.cause().getMessage());\r\n      }\r\n    });\r\n```\r\n\r\nIn the example, I am unsure when the queries will take place and in what order they will really come into play. Also, what happens if I do anything in the `ar -> {}` handlers? If I call a helper function that uses the current connection, does it know (does it need to know?) that it is currently in transaction mode?\r\n\r\nI would vote for another API, to make it more explicit and consistent with other, most probably asynchronous actions:\r\n\r\n```java\r\n    // Begin the transaction\r\n  conn.transaction(txAsyncResult -> {\r\n    // error handling omitted everywhere...\r\n    PgTransaction tx = txAsyncResult.result();\r\n\r\n    // Various statements, sequential instead of parallel here, could use the error handling with tx.rollback()\r\n    tx.query(\"INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')\", ar1 -> {\r\n      tx.query(\"INSERT INTO Users (first_name,last_name) VALUES ('Emad','Alblueshi')\", ar2 -> {\r\n        tx.commit(ar -> {\r\n          if (ar.succeeded()) {\r\n            System.out.println(\"Transaction succeeded\");\r\n          } else {\r\n            System.out.println(\"Transaction failed \" + ar.cause().getMessage());\r\n          }\r\n        });\r\n      });\r\n    });\r\n\r\n    // Commit the transaction\r\n    tx.commit(ar -> {\r\n      if (ar.succeeded()) {\r\n        System.out.println(\"Transaction succeeded\");\r\n      } else {\r\n        System.out.println(\"Transaction failed \" + ar.cause().getMessage());\r\n      }\r\n    });\r\n  });\r\n```\r\n\r\nMaybe we can get some inspiration from SQLite and the (stopped) [Web Database Specification](https://www.w3.org/TR/webdatabase/). I have used Cordovas JavaScript Database (I think it was SQLite) in earlier versions of Cordova and it was really easy to grasp and use due its consistency regarding \"always asynchronous actions\".\r\n\r\nIf we plan to return `Futures` instead of using callbacks, we can also `await` them quite nice in languages that support that feature... :)"},"comment":{"url":"https://api.github.com/repos/reactiverse/reactive-pg-client/issues/comments/402762466","html_url":"https://github.com/reactiverse/reactive-pg-client/issues/112#issuecomment-402762466","issue_url":"https://api.github.com/repos/reactiverse/reactive-pg-client/issues/112","id":402762466,"node_id":"MDEyOklzc3VlQ29tbWVudDQwMjc2MjQ2Ng==","user":{"login":"davidtrobinson","id":4791847,"node_id":"MDQ6VXNlcjQ3OTE4NDc=","avatar_url":"https://avatars0.githubusercontent.com/u/4791847?v=4","gravatar_id":"","url":"https://api.github.com/users/davidtrobinson","html_url":"https://github.com/davidtrobinson","followers_url":"https://api.github.com/users/davidtrobinson/followers","following_url":"https://api.github.com/users/davidtrobinson/following{/other_user}","gists_url":"https://api.github.com/users/davidtrobinson/gists{/gist_id}","starred_url":"https://api.github.com/users/davidtrobinson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidtrobinson/subscriptions","organizations_url":"https://api.github.com/users/davidtrobinson/orgs","repos_url":"https://api.github.com/users/davidtrobinson/repos","events_url":"https://api.github.com/users/davidtrobinson/events{/privacy}","received_events_url":"https://api.github.com/users/davidtrobinson/received_events","type":"User","site_admin":false},"created_at":"2018-07-05T15:32:09Z","updated_at":"2018-07-05T15:32:09Z","author_association":"NONE","body":"I don't know if this is a separate issue, but I really want to write a transactional query using RxJava2.  I can't find any examples in the Java docs and looking at other examples it does not seem like you can start a transaction and get an observable.  Is that related to transactions not being async?  \r\n\r\nI feel like transactional queries would benefit most from an observable chain since you often need the results of the previous statement to execute the next statement in a transaction. Writing each subsequent statement in the async handler of the previous statement lends itself to callback hell.\r\n\r\nThanks!"}}
{"actor":{"display_login":"davidtrobinson"}}