main()
{
createEntityManagerFactory(model.getDriver(), model.getUrl(), model.getUser(), model.getPassword());
}
@SuppressWarnings("unused")
public static void createEntityManagerFactory(String driver, String url, String username, String password) {
Map<String, String> persistenceMap = new HashMap<String, String>();
persistenceMap.put("javax.persistence.jdbc.driver", driver);
persistenceMap.put("javax.persistence.jdbc.url", url);
persistenceMap.put("javax.persistence.jdbc.user", username);
persistenceMap.put("javax.persistence.jdbc.password", password);
try {
if (entityManagerFactory == null)
entityManagerFactory = Persistence.createEntityManagerFactory("XYZAPI", persistenceMap);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
public static EntityManagerFactory getEntityManagerFactory() {
return entityManagerFactory;
}
这是我在服务器启动时调用的代码。
这是我的实体管理器功能。
public static EntityManager getDBManager() {
EntityManager entityManager = null;
EntityManagerFactory entityManagerFactory =
Configuration.getEntityManagerFactory();
if(entityManagerFactory != null){
entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
}
return entityManager;
}
但是当我第一次点击时,我得到的数据和响应能够连接数据库但是第二次当我点击时它会给出异常java.lang.IllegalStateException: EntityManagerFactory is closed
我在这里得到了例外
entityManager = entityManagerFactory.createEntityManager();
在这一行请建议我如何解决这个问题我必须在服务器启动时加载mysql用户ID和密码。