I'm using JPA for a desktop software. The end user needed a frame to change the Database connection dynamically (on the fly) by changing the persistance.xml data. I've tried using below code (which I found here) and modified but so far unsuccessful in changing the database connection.
EntityManagerFactory managerFactory = EntityManagerSingleton.getInstance().getEntityManagerFactory();
managerFactory = null;
Map<String, String> persistenceMap = new HashMap<String, String>();
persistenceMap.put("javax.persistence.jdbc.url", "jdbc:mysql://"+dbHost+":"+dbPort+"/"+dbName+"?zeroDateTimeBehavior=convertToNull");
persistenceMap.put("javax.persistence.jdbc.user", dbUser);
persistenceMap.put("javax.persistence.jdbc.password", dbPwd);
persistenceMap.put("javax.persistence.jdbc.driver","com.mysql.jdbc.Driver");
managerFactory = Persistence.createEntityManagerFactory("Persistance Unit Name", persistenceMap);
manager = managerFactory.createEntityManager();
I'm using a singleton class to get the entity manager factory so it will create only one connection to the database and only one Entity manager factory. Above managerFactory is the returned value from the singleton class. So can anyone tell me what I was doing wrong or a better way to do this?