首页 > 解决方案 > 在 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(); 
    }

标签: javamysqlspringjpaeclipselink

解决方案


推荐阅读