简体   繁体   中英

Can I create multiple Database Connections from one EntityManagerFactory?

I use JPA with Hibernate in my Servlet, which is hosted by Tomcat.
The database I use is MySQL.
I do not use JNDI or a Connection Pool.

This is my persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<persistence version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
    <persistence-unit name="pixxio-jpa" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>de.java2enterprise.onlineshop.model.AccessToken</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://192.168.88.88:3306/felix1_0"/>
            <property name="javax.persistence.jdbc.user" value="felix1_0"/>
            <property name="javax.persistence.jdbc.password" value="mypassword"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        </properties>
    </persistence-unit>
</persistence>

This is the important code in my Servlet:

void doubleDatabaseConnection(PrintWriter out) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("pixxio-jpa", null);
    EntityManager em = emf.createEntityManager();
    EntityManager em2 = emf.createEntityManager();
    getDatabaseSessionId(out, em);
    getDatabaseSessionId(out, em2);
    em.close();
    em2.close();
    emf.close();
}

void getDatabaseSessionId(PrintWriter out, EntityManager entityManager) {
    Query q = entityManager.createNativeQuery("SELECT CONNECTION_ID();");
    BigInteger result = (BigInteger) q.getSingleResult();

    out.println("<br>Database Session Id: " + result);
}

This is printed by the servlet:

Database Session Id: 51317
Database Session Id: 51317

I assumed that the actual database connection is established, when the EntityManager is created. Therefore I assumed that the two MySQL-Connection-IDs from the two EntityManager instances differ.

Is it possible to create multiple distinct database connections from one EntityManagerFactory instance?

I want to note that changing the JPA provider is an option for me.

Hibernate use the same connection if you do not need a transaction.

If you start a transation, you get a new connection

EntityManagerFactory emf = Persistence.createEntityManagerFactory("pixxio-jpa", null);

EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
EntityManager em2 = emf.createEntityManager();
em2.getTransaction().begin();
getDatabaseSessionId(System.out, em);
getDatabaseSessionId(System.out, em2);
em.close();
em2.close();
emf.close();

Updated

FYI: Hibernate use it's own connection pool if you do not set an connection pool (min=1; max=20).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM