Guest User

Untitled

a guest
Jan 17th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. public class SessionCustomizerImpl implements org.eclipse.persistence.config.SessionCustomizer {
  2.  
  3. private final static String WSREP_SYNC_WAIT_CHECK_SQL = "SHOW SESSION VARIABLES LIKE 'wsrep_sync_wait'";
  4. private final static String WSREP_SYNC_WAIT_SET_SQL = "SET SESSION wsrep_sync_wait = 1";
  5.  
  6. @Override
  7. public void customize(Session session) throws Exception {
  8. Vector result = session.executeSQL(WSREP_SYNC_WAIT_CHECK_SQL);
  9. if ((result != null) && !result.isEmpty()) {
  10. session.executeNonSelectingSQL(WSREP_SYNC_WAIT_SET_SQL);
  11. // Galera connection detected; wsrep_sync_wait set to 1
  12. } else {
  13. // No Galera connection detected; wsrep_sync_wait not set
  14. }
  15.  
  16. }
  17. }
  18.  
  19. public class SyncWaitEntityManagerFactory implements Factory<EntityManager> {
  20. private final EntityManagerFactory emf;
  21.  
  22. @Inject
  23. public SyncWaitEntityManagerFactory(EntityManagerFactory emf) {
  24. this.emf = emf;
  25. }
  26.  
  27. @Override
  28. public EntityManager provide() {
  29. final EntityManager em = emf.createEntityManager();
  30. // set it
  31. em.getTransaction().begin();
  32. em.createNativeQuery("SET SESSION wsrep_sync_wait = 1").executeUpdate();
  33. em.getTransaction().commit();
  34.  
  35. return em;
  36. }
  37.  
  38. @Override
  39. public void dispose(EntityManager instance) {
  40. if (instance.isOpen()) {
  41. instance.close();
  42. }
  43. }
  44.  
  45. }
  46.  
  47. String jdbcUrl = "jdbc:mysql://db.example.test:3306/"+ JDBC_DB
  48. +"?sessionVariables=wsrep_sync_wait=1";
  49. Properties p = new Properties();
  50. p.put("javax.persistence.jdbc.url", jdbcUrl);
  51. p.put("javax.persistence.jdbc.user", JDBC_USER);
  52. p.put("javax.persistence.jdbc.password", JDBC_PASSWORD);
  53. EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPU", p);
  54.  
  55. EntityManager entityManager = emf.createEntityManager();
Add Comment
Please, Sign In to add comment