A
A
Alexey Kopendakov2018-11-30 08:58:05
Java
Alexey Kopendakov, 2018-11-30 08:58:05

What is the right way to make it possible to select a database when the application starts?

Good afternoon.
The essence of the question is this.
There is a web application in which the database is registered through persistence.xml, the connection to the database is registered in the application server itself (wildfly), everything is fine, everything works. The problem is that the name of the database can be changed (the database is an archive of events, periodically (I still don’t understand why), a new database is created (with a new name), and the old one simply remains.
How should I proceed in this case? for good , I need to add the ability to choose which database to connect to, but I have absolutely no idea how to do this.
Which way to dig?
PS. I use the javaee stack. Spring is not used.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DS1977, 2018-11-30
@alex_kag

public class EntityManagerFactory {

    public static EntityManager get() {
        Map<String, Object> properties = new HashMap<>();
        properties.put("javax.persistence.jdbc.url", Config.DATABASE_URL);
        properties.put("javax.persistence.jdbc.driver", Config.DATABASE_DRIVER);
        properties.put("javax.persistence.jdbc.user", Config.DATABASE_USER);
        properties.put("javax.persistence.jdbc.password", Config.DATABASE_PWD);
        properties.put(PersistenceUnitProperties.WEAVING, "static");
        return new PersistenceProvider().createEntityManagerFactory("dbsource_pu", properties).createEntityManager();
    }
}

Y
Yerlan Ibraev, 2018-11-30
@mad_nazgul

JNDI?!
<:o)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question