Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SessionCustomizerImpl implements org.eclipse.persistence.config.SessionCustomizer {
- private final static String WSREP_SYNC_WAIT_CHECK_SQL = "SHOW SESSION VARIABLES LIKE 'wsrep_sync_wait'";
- private final static String WSREP_SYNC_WAIT_SET_SQL = "SET SESSION wsrep_sync_wait = 1";
- @Override
- public void customize(Session session) throws Exception {
- Vector result = session.executeSQL(WSREP_SYNC_WAIT_CHECK_SQL);
- if ((result != null) && !result.isEmpty()) {
- session.executeNonSelectingSQL(WSREP_SYNC_WAIT_SET_SQL);
- // Galera connection detected; wsrep_sync_wait set to 1
- } else {
- // No Galera connection detected; wsrep_sync_wait not set
- }
- }
- }
- public class SyncWaitEntityManagerFactory implements Factory<EntityManager> {
- private final EntityManagerFactory emf;
- @Inject
- public SyncWaitEntityManagerFactory(EntityManagerFactory emf) {
- this.emf = emf;
- }
- @Override
- public EntityManager provide() {
- final EntityManager em = emf.createEntityManager();
- // set it
- em.getTransaction().begin();
- em.createNativeQuery("SET SESSION wsrep_sync_wait = 1").executeUpdate();
- em.getTransaction().commit();
- return em;
- }
- @Override
- public void dispose(EntityManager instance) {
- if (instance.isOpen()) {
- instance.close();
- }
- }
- }
- String jdbcUrl = "jdbc:mysql://db.example.test:3306/"+ JDBC_DB
- +"?sessionVariables=wsrep_sync_wait=1";
- Properties p = new Properties();
- p.put("javax.persistence.jdbc.url", jdbcUrl);
- p.put("javax.persistence.jdbc.user", JDBC_USER);
- p.put("javax.persistence.jdbc.password", JDBC_PASSWORD);
- EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPU", p);
- EntityManager entityManager = emf.createEntityManager();
Add Comment
Please, Sign In to add comment