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

Test App: Add Java code for transaction retries #987

Merged
merged 1 commit into from
Jan 12, 2017

Conversation

rjnn
Copy link
Contributor

@rjnn rjnn commented Jan 11, 2017

Add example code for transaction retry logic. Compilation and
execution instructions are in a comment in the main code file.


This change is Reviewable

@rjnn rjnn requested a review from BramGruneir January 11, 2017 22:10
@rjnn rjnn force-pushed the add_java_txn_example_code branch 2 times, most recently from fa6cc52 to 3e91e90 Compare January 11, 2017 22:27
@BramGruneir
Copy link
Member

Reviewed 3 of 3 files at r1.
Review status: all files reviewed at latest revision, 5 unresolved discussions.


_includes/app/TxnSample.java, line 19 at r1 (raw file):

    public static RetryableTransaction transferFunds(int from, int to, int amount) {
        return new RetryableTransaction() {
            public void run(Connection conn) throws Exception {

Won't this only throw SQLException and InsufficientBalanceException? Why not just list only those two?


_includes/app/TxnSample.java, line 21 at r1 (raw file):

            public void run(Connection conn) throws Exception {
                ResultSet res = conn.createStatement().executeQuery("SELECT balance FROM accounts WHERE id = " + from);
                res.next();

Why do you need the next here? Shouldn't you already be at the first result?
What does the resultSet look like? Should you check to make sure it returns true?


_includes/app/TxnSample.java, line 32 at r1 (raw file):

    }

    public static void retryTransaction(Connection conn, RetryableTransaction tx) throws Exception {

Again here, list only the exceptions that you're going to throw.


_includes/app/TxnSample.java, line 66 at r1 (raw file):

                db.setAutoCommit(false);

                RetryableTransaction transfer = transferFunds(1, 2, 100);

So this assumes there's already a db with a table, with two accounts setup. Perhaps add a comment about the initial setup above.


_includes/app/TxnSample.java, line 72 at r1 (raw file):

                    System.out.printf("\taccount %s: %s\n", res.getInt("id"), res.getInt("balance"));
                }
            } catch(InsufficientBalanceException ibe) {

From what I've seen, these are all usually named just e. Unless you're got nested try catches. And above, you just use e.


Comments from Reviewable

@rjnn
Copy link
Contributor Author

rjnn commented Jan 12, 2017

Thank you for the review!


Review status: 2 of 3 files reviewed at latest revision, 5 unresolved discussions.


_includes/app/TxnSample.java, line 19 at r1 (raw file):

Previously, BramGruneir (Bram Gruneir) wrote…

Won't this only throw SQLException and InsufficientBalanceException? Why not just list only those two?

Done.


_includes/app/TxnSample.java, line 21 at r1 (raw file):

Previously, BramGruneir (Bram Gruneir) wrote…

Why do you need the next here? Shouldn't you already be at the first result?
What does the resultSet look like? Should you check to make sure it returns true?

The Java ResultSet spec says A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row.
Added a check that it returns true.


_includes/app/TxnSample.java, line 32 at r1 (raw file):

Previously, BramGruneir (Bram Gruneir) wrote…

Again here, list only the exceptions that you're going to throw.

Done.


_includes/app/TxnSample.java, line 66 at r1 (raw file):

Previously, BramGruneir (Bram Gruneir) wrote…

So this assumes there's already a db with a table, with two accounts setup. Perhaps add a comment about the initial setup above.

Added.


_includes/app/TxnSample.java, line 72 at r1 (raw file):

Previously, BramGruneir (Bram Gruneir) wrote…

From what I've seen, these are all usually named just e. Unless you're got nested try catches. And above, you just use e.

Done.


Comments from Reviewable

@BramGruneir
Copy link
Member

:lgtm:

A few final nits, but looks good.

Reviewed 1 of 1 files at r2.
Review status: all files reviewed at latest revision, 1 unresolved discussion.


_includes/app/TxnSample.java, line 21 at r1 (raw file):

Previously, arjunravinarayan (Arjun Narayan) wrote…

The Java ResultSet spec says A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row.
Added a check that it returns true.

If you want the first element, why not use first? And on failure, you probably want another exception, maybe a accountNotFoundException (that takes an account number) or something like that to match the insufficientBalanceException.


Comments from Reviewable

@rjnn
Copy link
Contributor Author

rjnn commented Jan 12, 2017

Thank you!


Review status: 2 of 3 files reviewed at latest revision, 1 unresolved discussion.


_includes/app/TxnSample.java, line 21 at r1 (raw file):

Previously, BramGruneir (Bram Gruneir) wrote…

If you want the first element, why not use first? And on failure, you probably want another exception, maybe a accountNotFoundException (that takes an account number) or something like that to match the insufficientBalanceException.

The previous example program in the docs uses next() for this so consistency. I'm ambivalent either way.

Added the other exception. Even added a constructor for it and everything, just for MAXIMUM JAVANESS. Let me know if you would like an ExceptionFactory too :)


Comments from Reviewable

@rjnn
Copy link
Contributor Author

rjnn commented Jan 12, 2017

@jseldess happy to make stylistic changes as you would like, or add/improve the comments. I'll wait for your approval before merging.

Copy link
Contributor

@jseldess jseldess left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just some suggestions to add comments similar to our other samples. Our commenting eventually needs to be made more consistent across samples, but this should be good for now.

</div>
~~~ java
{% include app/TxnSample.java %}
~~~

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to put the closing </div> back in. Otherwise, the other transaction samples don't show up.

if(balance < from) {
throw new InsufficientBalanceException();
}
conn.createStatement().executeUpdate("UPDATE accounts SET balance = balance - " + amount + " where id = " + from);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add comment before line 28: # Perform the transfer.

public static RetryableTransaction transferFunds(int from, int to, int amount) {
return new RetryableTransaction() {
public void run(Connection conn) throws SQLException, InsufficientBalanceException {
ResultSet res = conn.createStatement().executeQuery("SELECT balance FROM accounts WHERE id = " + from);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add comment before line 20: # Check the current balance.

// This assumes the table has already been set up as in the "Build a Test App" tutorial.
RetryableTransaction transfer = transferFunds(1, 2, 100);
retryTransaction(db, transfer);
ResultSet res = db.createStatement().executeQuery("SELECT id, balance FROM accounts");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add comment before line 70: # Check balances after transfer.

@BramGruneir
Copy link
Member

Reviewed 1 of 1 files at r3.
Review status: all files reviewed at latest revision, 5 unresolved discussions.


_includes/app/TxnSample.java, line 21 at r1 (raw file):

Previously, arjunravinarayan (Arjun Narayan) wrote…

The previous example program in the docs uses next() for this so consistency. I'm ambivalent either way.

Added the other exception. Even added a constructor for it and everything, just for MAXIMUM JAVANESS. Let me know if you would like an ExceptionFactory too :)

For MAXIMUM JAVANESS, you need to add an abstract factory for that factory and use dependency injection to inform it at runtime as to what type of factory to make.


Comments from Reviewable

Add example code for transaction retry logic. Compilation and
execution instructions are in a comment in the main code file.
Fixes cockroachdb#757.
@rjnn
Copy link
Contributor Author

rjnn commented Jan 12, 2017

Review status: 1 of 3 files reviewed at latest revision, 4 unresolved discussions.


build-a-test-app.md, line 219 at r2 (raw file):

Previously, jseldess wrote…

We need to put the closing </div> back in. Otherwise, the other transaction samples don't show up.

Done.


_includes/app/TxnSample.java, line 20 at r2 (raw file):

Previously, jseldess wrote…

Maybe add comment before line 20: # Check the current balance.

Done.


_includes/app/TxnSample.java, line 28 at r2 (raw file):

Previously, jseldess wrote…

Maybe add comment before line 28: # Perform the transfer.

Done.


_includes/app/TxnSample.java, line 70 at r2 (raw file):

Previously, jseldess wrote…

Maybe add comment before line 70: # Check balances after transfer.

Done.


Comments from Reviewable

@rjnn rjnn merged commit b012250 into cockroachdb:gh-pages Jan 12, 2017
@rjnn rjnn deleted the add_java_txn_example_code branch January 12, 2017 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants