Skip to content

Conversation

rmloveland
Copy link
Contributor

Fixes #5537.

Relates to #1775, #4810, #5035.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rmloveland rmloveland changed the title Update Hibernate example with app-level rety loop Update Hibernate example with app-level retry loop Oct 31, 2019
@cockroach-teamcity
Copy link
Member

@rmloveland rmloveland force-pushed the 20191031-hibernate-app-retry-loop branch from 963c25d to 1bf4846 Compare October 31, 2019 20:25
@cockroach-teamcity
Copy link
Member

@rmloveland rmloveland requested a review from rafiss October 31, 2019 20:43
@rmloveland
Copy link
Contributor Author

Hey @rafiss I think the core of the retry loop is working well based on my local testing, but I feel like this code needs updates that would make it more idiomatic for real Hibernate users. PTAL and let me know what you think.

(FYI I'm only updating the "insecure" example in this PR to keep it small while we fix the code and content - once we're happyish with it I'll also update the "secure" example and descriptions.)

@rmloveland
Copy link
Contributor Author

@rafiss, ping. IDK if this can be ready for the release tomorrow but I'd love to get it in this week if at all possible!

Copy link
Contributor

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

Sorry for the delay on the review! happy to sit next to you and show you my comments, or if it would help, i could even make some code changes myself, and it might be quicker. just let me know what works best for you! :)

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss and @rmloveland)


_includes/v19.2/app/insecure/hibernate-basic-sample/Sample.java, line 57 at r1 (raw file):

    }

    private static Random rand = new Random();

should be private static final


_includes/v19.2/app/insecure/hibernate-basic-sample/Sample.java, line 62 at r1 (raw file):

    static Function addAccounts(Session s1) throws JDBCException{
        Function f = (Function<Session, Integer>) s -> {
            Integer rv = 0;

replace these lines with

   private static Function<Session, Long> addAccounts() throws JDBCException{
        Function<Session, Long> f = s -> {
            long rv = 0;

to explain the changes:

  • function should be private
  • no need for the Session s1 parameter
  • the <...> syntax should be used in the return type
  • and also in the first line
  • this avoids the need for the cast
  • use long instead of Integer

_includes/v19.2/app/insecure/hibernate-basic-sample/Sample.java, line 70 at r1 (raw file):

                System.out.printf("APP: addAccounts() --> %d\n", rv);
            } catch (JDBCException e) {
                rv = -1;

remove rv = -1

that doesn't get used anywhere


_includes/v19.2/app/insecure/hibernate-basic-sample/Sample.java, line 80 at r1 (raw file):

    static Function transferFunds(long fromId, long toId, long amount) throws JDBCException{
        Function f = (Function<Session, Long>) s -> {
            long rv = 0;

replace these with

    private static Function<Session, Long> transferFunds(long fromId, long toId, long amount) throws JDBCException{
        Function<Session, Long> f = s -> {
            long rv = 0;

_includes/v19.2/app/insecure/hibernate-basic-sample/Sample.java, line 93 at r1 (raw file):

                }
            } catch (JDBCException e) {
                rv = -1;

remove rv = -1


_includes/v19.2/app/insecure/hibernate-basic-sample/Sample.java, line 104 at r1 (raw file):

    static Function testRetryLogic() throws JDBCException {
        Function f = (Function<Session, Integer>) s -> {
            Integer rv = -1;

replace with

    private static Function<Session, Long> forceRetryLogic() throws JDBCException {
        Function<Session, Long> f = s -> {
            long rv = -1;

(note that i renamed the function name. having "test" in the name is misleading)


_includes/v19.2/app/insecure/hibernate-basic-sample/Sample.java, line 122 at r1 (raw file):

Quoted 5 lines of code…
    static Function getAccountBalance(long id) throws JDBCException{
        Function f = (Function<Session, Long>) s -> {
            long balance;
            try {
                Account account = (Account) s.get(Account.class, id);

replace these with

    private static Function<Session, Long> getAccountBalance(long id) throws JDBCException{
        Function<Session, Long> f = s -> {
            long balance;
            try {
                Account account = s.get(Account.class, id);

_includes/v19.2/app/insecure/hibernate-basic-sample/Sample.java, line 138 at r1 (raw file):

    private static Number runTransaction(Session session, Function<Session, ? extends Number> fn) {
        Number rv = 0;

replace this with

    private static long runTransaction(Session session, Function<Session, Long> fn) {
        long rv = 0;

_includes/v19.2/app/insecure/hibernate-basic-sample/Sample.java, line 154 at r1 (raw file):

Quoted 4 lines of code…
            if (retryCount == MAX_RETRY_COUNT) {
                String err = String.format("hit max of %s retries, aborting", MAX_RETRY_COUNT);
                throw new RuntimeException(err);
            }

this block of code will never be reached. to fix this, i would rename retryCount to attemptCount, and increment attemptCount as the first thing in the loop. (and remove the incrementing in the catch block below)


_includes/v19.2/app/insecure/hibernate-basic-sample/Sample.java, line 179 at r1 (raw file):

                    System.out.printf("APP: retryable exception occurred:\n    sql state = [%s]\n    message = [%s]\n    retry counter = %s\n", e.getSQLState(), e.getMessage(), retryCount);
                    System.out.printf("APP: ROLLBACK;\n");
                    retryCount++;

remove this retryCount++ line if you take my suggestion above


_includes/v19.2/app/insecure/hibernate-basic-sample/Sample.java, line 190 at r1 (raw file):

                    rv = -1;
                } else {
                    rv = -1;

no need for this rv = -1 line


_includes/v19.2/app/insecure/hibernate-basic-sample/Sample.java, line 213 at r1 (raw file):

                Number fromBalance = runTransaction(session, getAccountBalance(fromAccountId));
                Number toBalance = runTransaction(session, getAccountBalance(toAccountId));
                if (fromBalance.intValue() != -1 && toBalance.intValue() != -1) {

remove all the intValue() calls everywhere in the code. it's not necessary and is kind of unconventional

Copy link
Contributor Author

@rmloveland rmloveland left a comment

Choose a reason for hiding this comment

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

i could even make some code changes myself, and it might be quicker

Honestly if you really don't mind, I agree that would be faster than me fumbling through this and fixing what I would inevitably break

Feel free to force push away :-)

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rmloveland)

@rafiss rafiss force-pushed the 20191031-hibernate-app-retry-loop branch from 1bf4846 to f96be7a Compare November 21, 2019 21:04
Copy link
Contributor

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

Got it! I have force pushed my changes, so this is good to merge from my end whenever you have everything else ready

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rmloveland)

@cockroach-teamcity
Copy link
Member

@rmloveland
Copy link
Contributor Author

Thanks @rafiss !!! I really appreciate your help.

I will now start the following tasks which will generate a bit of noise, but none of them should require any effort on your part; they're mostly editing-related tasks:

  • Update the "secure" version with this code and these docs
  • Copy these changes to the 19.1 and the 20.1 docs as well
  • Kick off the docs team review process so we can merge this :-)

@rmloveland rmloveland force-pushed the 20191031-hibernate-app-retry-loop branch from f96be7a to 08895ba Compare November 22, 2019 17:31
@rmloveland
Copy link
Contributor Author

@ericharmeling this one is a little complicated logistically - would appreciate your careful review. The actual updates are pretty much the same for 19.1, 19.2, and 20.1. My main concern is ensuring that I did not insert any subtle errors to the docs pages when copying all the different files around, and rebasing my local branch atop the many changes to master that have occurred since this PR was opened. There is also some complexity around the "Secure" vs. "Insecure" sections of each page, and ensuring that links point to the correct versions of the Hibernate app tarballs. (Really looking forward to avoiding a lot of this pain from now on as soon as #5885 gets done!)

I'll be OOO next week but will jump on any review comments first thing the Monday AM after that!

@rmloveland
Copy link
Contributor Author

rmloveland commented Dec 5, 2019 via email

Copy link
Contributor

@ericharmeling ericharmeling left a comment

Choose a reason for hiding this comment

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

Thanks for the context, @rmloveland.

All of the example code is identical across versions with the following exceptions:

  • No changes to _includes/v19.1/app/insecure/hibernate-basic-sample are part of this PR. Are we not updating the insecure example for 19.1?
  • _includes/v20.1/app/insecure/hibernate-basic-sample/hibernate.cfg.xml includes a secure connection string. Shouldn't this be identical to _includes/v19.2/app/insecure/hibernate-basic-sample/hibernate.cfg.xml?

Also, unless I am missing something, there are a lot of "insecure" links and commands in secure "Build an App" doc sections (mainly in 19.1 and 20.1).

Reviewed 1 of 4 files at r1, 16 of 17 files at r2.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ericharmeling and @rmloveland)


_includes/v20.1/app/insecure/hibernate-basic-sample/hibernate.cfg.xml, line 11 at r2 (raw file):

true&sslmode=require&sslrootcert=certs/ca.crt&sslkey=certs/client.maxroach.key.pk8&sslcert=certs/client.maxroach.crt]]

Isn't this an insecure example?


v19.1/build-a-java-app-with-cockroachdb-hibernate.md, line 88 at r2 (raw file):

1. Download and extract [hibernate-basic-sample.tgz](https://github.com/cockroachdb/docs/raw/master/_includes/{{ page.version.version }}/app/insecure/hibernate-basic-sample/hibernate-basic-sample.tgz). The settings in [`hibernate.cfg.xml`](https://github.com/cockroachdb/docs/raw/master/_includes/{{ page.version.version }}/app/insecure/hibernate-basic-sample/hibernate.cfg.xml) specify how to connect to the database.
2. Compile and run the code using [`build.gradle`](https://github.com/cockroachdb/docs/raw/master/_includes/{{ page.version.version }}/app/insecure/hibernate-basic-sample/build.gradle), which will also download the dependencies.

These are all "insecure" files, and we are still in the secure section of the doc.


v19.1/build-a-java-app-with-cockroachdb-hibernate.md, line 96 at r2 (raw file):

The contents of [`Sample.java`](https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/{{page.version.version}}/app/insecure/hibernate-basic-sample/Sample.java):

We are still in the secure section of the doc.


v19.1/build-a-java-app-with-cockroachdb-hibernate.md, line 100 at r2 (raw file):

{% include {{page.version.version}}/app/insecure/hibernate-basic-sample/Sample.java %}

We are still in the secure section of the doc.


v19.1/build-a-java-app-with-cockroachdb-hibernate.md, line 135 at r2 (raw file):

cockroach sql --insecure --database=bank

We are still in the secure section of the doc.


v20.1/build-a-java-app-with-cockroachdb-hibernate.md, line 88 at r2 (raw file):

1. Download and extract [hibernate-basic-sample.tgz](https://github.com/cockroachdb/docs/raw/master/_includes/{{ page.version.version }}/app/insecure/hibernate-basic-sample/hibernate-basic-sample.tgz). The settings in [`hibernate.cfg.xml`](https://github.com/cockroachdb/docs/raw/master/_includes/{{ page.version.version }}/app/insecure/hibernate-basic-sample/hibernate.cfg.xml) specify how to connect to the database.
2. Compile and run the code using [`build.gradle`](https://github.com/cockroachdb/docs/raw/master/_includes/{{ page.version.version }}/app/insecure/hibernate-basic-sample/build.gradle), which will also download the dependencies.

We are still in the secure section of the doc.


v20.1/build-a-java-app-with-cockroachdb-hibernate.md, line 96 at r2 (raw file):


The contents of [`Sample.java`](https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/{{page.version.version}}/app/insecure/hibernate-basic-sample/Sample.java):

We are still in the secure section of the doc.


v20.1/build-a-java-app-with-cockroachdb-hibernate.md, line 100 at r2 (raw file):

{% include {{page.version.version}}/app/insecure/hibernate-basic-sample/Sample.java %}

We are still in the secure section of the doc.


v20.1/build-a-java-app-with-cockroachdb-hibernate.md, line 295 at r2 (raw file):

<span class="version-tag">New in v19.2:</span>

Should remove this from 20.1 docs.

Copy link
Contributor

@ericharmeling ericharmeling left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 17 files at r2.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rmloveland)

@rmloveland rmloveland force-pushed the 20191031-hibernate-app-retry-loop branch from 08895ba to e7091be Compare December 12, 2019 17:22
@rmloveland
Copy link
Contributor Author

Thanks for the review @ericharmeling ! I have fixed the issues you discovered. Please take a look and let me know if you think this is good to go.

Copy link
Contributor

@ericharmeling ericharmeling left a comment

Choose a reason for hiding this comment

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

LGTM!

Thanks !!

Reviewed 6 of 6 files at r3.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rmloveland)

@rmloveland rmloveland merged commit 7d4210e into master Dec 17, 2019
@rmloveland rmloveland deleted the 20191031-hibernate-app-retry-loop branch December 17, 2019 20:27
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.

App retry loop example for Hibernate
4 participants