How to use multiple database in persistence.xml?

The Java Persistence API allows you to define multiple persistence units, each of which can map to a separate database.

In order to use multiple Database, simply define a persistent-unit for each one in the persistence.xml file :

    <persistence-unit name="sample-db1">
    <persistence-unit name="sample-db2">

In te above example, the sample-db1 and sample-db2 persistence units have been configured in the persistence.xml file.

You can use @PersistenceContext attribute unitName to specify which persistent unit to use in your code:

public class EmployeeDemoSessionEJB implements EmployeeDemoSession {

    protected EntityManager em1;

    protected EntityManager em2;

    public void createEmployee(String fName, String lName) {
        Employee employee  = new Employee();