Advertisement
rmloveland

Hibernate app retry loop - can't get lambda to compile

Aug 13th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.96 KB | None | 0 0
  1. // Context: I'm trying to pass a lambda as a higher order function to a `runTransaction` wrapper method.
  2. // It's failing because of "bad return type".  I can't tell if my error is fundamental or syntax or both?
  3.  
  4. // Version info:
  5.  
  6. // java version "1.8.0_171"
  7. // Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
  8. // Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
  9.  
  10. // Error message:
  11.  
  12. // Sample.java:100: error: incompatible types: bad return type in lambda expression
  13. //             int returnValue = runTransaction(session, (Session s) -> {
  14. //                                                       ^
  15. //     missing return value
  16.  
  17.  
  18. package com.cockroachlabs;
  19.  
  20. import org.hibernate.Session;
  21. import org.hibernate.SessionFactory;
  22. import org.hibernate.Transaction;
  23. import org.hibernate.cfg.Configuration;
  24.  
  25. import java.util.*;
  26. import java.util.function.Function;
  27.  
  28. import javax.persistence.Column;
  29. import javax.persistence.Entity;
  30. import javax.persistence.Id;
  31. import javax.persistence.Table;
  32. import javax.persistence.criteria.CriteriaQuery;
  33.  
  34. public class Sample {
  35.     // Create a SessionFactory based on our hibernate.cfg.xml configuration
  36.     // file, which defines how to connect to the database.
  37.     private static final SessionFactory sessionFactory =
  38.             new Configuration()
  39.                     .configure("hibernate.cfg.xml")
  40.                     .addAnnotatedClass(Account.class)
  41.                     .buildSessionFactory();
  42.  
  43.     // Account is our model, which corresponds to the "accounts" database table.
  44.     @Entity
  45.     @Table(name="accounts")
  46.     public static class Account {
  47.         @Id
  48.         @Column(name="id")
  49.         public long id;
  50.  
  51.         @Column(name="balance")
  52.         public long balance;
  53.  
  54.         // Convenience constructor.
  55.         public Account(int id, int balance) {
  56.             this.id = id;
  57.             this.balance = balance;
  58.         }
  59.  
  60.         // Hibernate needs a default (no-arg) constructor to create model objects.
  61.         public Account() {}
  62.     }
  63.  
  64.     private static Random rand = new Random();
  65.  
  66.     // static Function addAccounts(Session s1) {
  67.     //     Function f = (Session s) -> {
  68.     //         try {
  69.     //             s.save(new Account(1, 1000));
  70.     //             s.save(new Account(2, 250));
  71.     //             s.save(new Account(3, 314159));
  72.     //             s.getTransaction().commit();
  73.     //         } catch (Exception e) {
  74.     //             throw e;
  75.     //         }
  76.     //     };
  77.     //     return f;
  78.     // }
  79.  
  80.  
  81.     static int runTransaction(Session session, Function fn) {
  82.  
  83.         int returnValue = 0;
  84.         int retryCount = 0;
  85.         int MAX_RETRY_COUNT = 3;
  86.         String RETRY_SQL_STATE = "40001";
  87.         boolean FORCE_RETRY = false;
  88.  
  89.         try {
  90.             fn.apply(session);
  91.         } catch (org.hibernate.JDBCException e) {
  92.  
  93.             if (RETRY_SQL_STATE.equals(e.getSQLState())) {
  94.                 session.getTransaction().rollback();
  95.                 retryCount++;
  96.                 int sleepMillis = (int)(Math.pow(2, retryCount) * 100) + rand.nextInt(100);
  97.                 System.out.printf("Hit 40001 transaction retry error, sleeping %s milliseconds\n", sleepMillis);
  98.                 try {
  99.                     Thread.sleep(sleepMillis);
  100.                 } catch (InterruptedException ignored) {
  101.                     // Necessary to allow the retry loop to continue.
  102.                 }
  103.                 returnValue = -1;
  104.             } else {
  105.                 returnValue = -1;
  106.                 throw e;
  107.             }
  108.         } finally {
  109.             session.close();
  110.             sessionFactory.close();
  111.         }
  112.     }
  113.  
  114.     public static void main(String[] args) throws Exception {
  115.         Session session = sessionFactory.openSession();
  116.         try {
  117. // Below lambda `(Session s) -> {` fails with:
  118. // error: incompatible types: bad return type in lambda expression
  119. //             int returnValue = runTransaction(session, (Session s) -> {
  120. //                                                       ^
  121. //     missing return value
  122.             int returnValue = runTransaction(session, (Session s) -> {  // BOOM!!!
  123.                     try {
  124.                         s.save(new Account(1, 1000));
  125.                         s.save(new Account(2, 250));
  126.                         s.save(new Account(3, 314159));
  127.                         s.getTransaction().commit();
  128.                     } catch (Exception e) {
  129.                         throw e;
  130.                     }
  131.                 });
  132.             if (returnValue != -1) {
  133.                 // Success!
  134.                 System.out.printf("transferFunds EXIT_SUCCESS");
  135.             }
  136.         } finally {
  137.             session.close();
  138.             sessionFactory.close();
  139.         }
  140.     }
  141. }
  142.  
  143. // gradle build
  144.  
  145. // > Task :compileJava FAILED
  146. // /Users/rloveland/work/code/deemphasize-savepoints/java-hibernate/src/main/java/com/cockroachlabs/Sample.java:100: error: incompatible types: bad return type in lambda expression
  147. //             int returnValue = runTransaction(session, (Session s) -> {
  148. //                                                       ^
  149. //     missing return value
  150. // Note: /Users/rloveland/work/code/deemphasize-savepoints/java-hibernate/src/main/java/com/cockroachlabs/Sample.java uses unchecked or unsafe operations.
  151. // Note: Recompile with -Xlint:unchecked for details.
  152. // Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
  153. // 1 error
  154.  
  155. // FAILURE: Build failed with an exception.
  156.  
  157. // * What went wrong:
  158. // Execution failed for task ':compileJava'.
  159. // > Compilation failed; see the compiler error output for details.
  160.  
  161. // * Try:
  162. // Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
  163.  
  164. // * Get more help at https://help.gradle.org
  165.  
  166. // BUILD FAILED in 564ms
  167. // 1 actionable task: 1 executed
  168.  
  169. // Compilation exited abnormally with code 1 at Tue Aug 13 13:55:17
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement