java - 在 Spring 和 Eclipse Link JPA 中使用连接池创建 Entity Manger 运行时
问题描述
我正在尝试在运行时创建一个具有连接池的实体管理器。数据库详细信息将由用户提供,基于此我们需要创建一个具有连接池的实体管理器。我正在使用 spring MVC,Eclipse Link JPA。我尝试了一些解决方案,但是他们正在使用 @configuration 注释,这对我来说并不理想。一种解决方案工作正常,但我无法维护连接池。下面是运行良好但无法添加对连接池的支持的代码。
public static EntityManager getEntityManager(){
EntityManagerFactory emf;
Map properties = new HashMap();
properties.put("eclipselink.jdbc.driver", "com.mysql.jdbc.Driver");
properties.put("eclipselink.jdbc.url", "jdbc:mysql://localhost:3306/SampleDB");
properties.put("eclipselink.jdbc.user", "root");
properties.put("eclipselink.jdbc.password", "root");
//emf = Persistence.createEntityManagerFactory("jpablogPUnit");
emf = Persistence.createEntityManagerFactory("datajpa",properties);
return (EntityManager) emf.createEntityManager();
}
解决方案
推荐阅读
- typescript - 如何从类型中提取不可为空的属性名称?
- django - Django表单:如何在下拉列表中仅显示与用户关联的对象
- python - 在数据帧上乘向量 - 向量化
- javascript - 如何使用 web 服务从 asp.net 中的数据库中填充“选择”
- python - 如何从 setup_require 依赖项覆盖 setuptools 命令?
- php - 如何使用 php 创建摄氏度到华氏度的转换表?
- hibernate - JPA-Hibernate 一对一 - 在这种情况下延迟加载真的有效吗?
- bash - 问题:在 docker 中运行 ubuntu bash shell 时出错
- json - Aeson:解析 json 对象以列出
- html - 并排的两个内容块