- Notifications
You must be signed in to change notification settings - Fork 33
Add TransactionListener interface #71
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits Select commit Hold shift + click to select a range
cd69b9f Add TransactionListener interface
aboisvert c1222cb Add ability to add listeners at DbClient/Conneciton level; add begin(…
aboisvert 77e008e Fixup: Scalafix-required code cleanup
aboisvert 375e8a3 Replace DbClient.addTransactionListener with DataSource.withTransacti…
aboisvert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -136,6 +136,45 @@ object DbApi { | |
| flattened.renderSql(castParams) | ||
| } | ||
| | ||
| /** | ||
| * A listener that can be added to a [[DbApi.Txn]] to be notified of commit and rollback events. | ||
| * | ||
| * The default implementations of these methods do nothing, but you can override them to | ||
| * implement your own behavior. | ||
| */ | ||
| trait TransactionListener { | ||
| | ||
| /** | ||
| * Called before the transaction is committed. | ||
| * | ||
| * If this method throws an exception, the transaction will be rolled back and the exception | ||
| * will be propagated. | ||
| */ | ||
| def beforeCommit(): Unit = () | ||
| | ||
| /** | ||
| * Called after the transaction is committed. | ||
| * | ||
| * If this method throws an exception, it will be propagated. | ||
| */ | ||
| def afterCommit(): Unit = () | ||
| | ||
| /** | ||
| * Called before the transaction is rolled back. | ||
| * | ||
| * If this method throws an exception, the transaction will be rolled back and the exception | ||
| * will be propagated to the caller of rollback(). | ||
| */ | ||
| def beforeRollback(): Unit = () | ||
| | ||
| /** | ||
| * Called after the transaction is rolled back. | ||
| * | ||
| * If this method throws an exception, it will be propagated to the caller of rollback(). | ||
| */ | ||
| def afterRollback(): Unit = () | ||
| } | ||
| | ||
| /** | ||
| * An interface to a SQL database *transaction*, allowing you to run queries, | ||
| * create savepoints, or roll back the transaction. | ||
| | @@ -151,9 +190,11 @@ object DbApi { | |
| def savepoint[T](block: DbApi.Savepoint => T): T | ||
| | ||
| /** | ||
| * Tolls back any active Savepoints and then rolls back this Transaction | ||
| * Rolls back any active Savepoints and then rolls back this Transaction | ||
| */ | ||
| def rollback(): Unit | ||
| | ||
| def addTransactionListener(listener: TransactionListener): Unit | ||
| } | ||
| | ||
| /** | ||
| | @@ -187,9 +228,16 @@ object DbApi { | |
| connection: java.sql.Connection, | ||
| config: Config, | ||
| dialect: DialectConfig, | ||
| autoCommit: Boolean, | ||
| rollBack0: () => Unit | ||
| autoCommit: Boolean | ||
| ) extends DbApi.Txn { | ||
| val listeners = collection.mutable.ArrayDeque.empty[TransactionListener] | ||
| ||
| | ||
| override def addTransactionListener(listener: TransactionListener): Unit = { | ||
| if (autoCommit) | ||
| throw new IllegalStateException("Cannot add listener to auto-commit transaction") | ||
| listeners.append(listener) | ||
| } | ||
| | ||
| def run[Q, R](query: Q, fetchSize: Int = -1, queryTimeoutSeconds: Int = -1)( | ||
| implicit qr: Queryable[Q, R], | ||
| fileName: sourcecode.FileName, | ||
| | @@ -218,6 +266,7 @@ object DbApi { | |
| res.toVector.asInstanceOf[R] | ||
| } | ||
| } | ||
| | ||
| } | ||
| | ||
| def stream[Q, R](query: Q, fetchSize: Int = -1, queryTimeoutSeconds: Int = -1)( | ||
| | @@ -229,8 +278,8 @@ object DbApi { | |
| streamFlattened0( | ||
| r => { | ||
| qr.asInstanceOf[Queryable[Q, R]].construct(query, r) match { | ||
| case s: Seq[R] => s.head | ||
| case r: R => r | ||
| case s: Seq[R] @unchecked => s.head | ||
| case r: R @unchecked => r | ||
| } | ||
| }, | ||
| flattened, | ||
| | @@ -545,8 +594,13 @@ object DbApi { | |
| } | ||
| | ||
| def rollback() = { | ||
| savepointStack.clear() | ||
| rollBack0() | ||
| try { | ||
| listeners.foreach(_.beforeRollback()) | ||
| } finally { | ||
| savepointStack.clear() | ||
| connection.rollback() | ||
| listeners.foreach(_.afterRollback()) | ||
| } | ||
| } | ||
| | ||
| private def cast[T](t: Any): T = t.asInstanceOf[T] | ||
| | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed the
rollBack0argument since it was only used to handle the difference betweenautoCommittrue/false behavior.With the new interface, client code can now hook into the rollback process if needed.