开发者

How do I load database credentials from a properties file in JPA?

开发者 https://www.devze.com 2023-03-19 04:19 出处:网络
So I have a Java/JPA2.0 (EclipseLink) app that conne开发者_运维百科cts to a MySQL database.My intention is just passing a JAR file around with a db.properties file.The db.properties should contain the

So I have a Java/JPA2.0 (EclipseLink) app that conne开发者_运维百科cts to a MySQL database. My intention is just passing a JAR file around with a db.properties file. The db.properties should contain the server host address, username, password, etc so that the end user can just plug that in and start using the JAR in their projects.

Currently, I just used Netbeans to create a persistence.xml file with the credentials and that works. But how do I implement the properties file?

My EntityManager class:

public class Factories {

    private static final EntityManagerFactory entityManagerFactory = buildEntityManagerFactory();

        private static EntityManagerFactory buildEntityManagerFactory() {
            try {
                return Persistence.createEntityManagerFactory("MyPU");
            } catch (Exception ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }
}

Thanks


You can use the two-parameter version of the createEntityManagerFactory() method. The second argument (the Map) can be used to pass properties including the credentials to the database. You can therefore pass in a map with keys javax.persistence.jdbc.user and javax.persistence.jdbc.password and appropriate values.

An example in the EclipseLink wiki demonstrates how to achieve this, although it uses classes provided by EclipseLink to achieve this:

import static org.eclipse.persistence.config.PersistenceUnitProperties.*;

    ...

    Map properties = new HashMap();

    // Ensure RESOURCE_LOCAL transactions is used.
    properties.put(TRANSACTION_TYPE,
        PersistenceUnitTransactionType.RESOURCE_LOCAL.name());

    // Configure the internal EclipseLink connection pool
    properties.put(JDBC_DRIVER, "oracle.jdbc.OracleDriver");
    properties.put(JDBC_URL, "jdbc:oracle:thin:@localhost:1521:ORCL");
    properties.put(JDBC_USER, "scott");
    properties.put(JDBC_PASSWORD, "tiger");

    // Configure logging. FINE ensures all SQL is shown
    properties.put(LOGGING_LEVEL, "FINE");
    properties.put(LOGGING_TIMESTAMP, "false");
    properties.put(LOGGING_THREAD, "false");
    properties.put(LOGGING_SESSION, "false");

    // Ensure that no server-platform is configured
    properties.put(TARGET_SERVER, TargetServer.None);

    // Now the EntityManagerFactory can be instantiated for testing using: 
    Persistence.createEntityManagerFactory(unitName, properties);

Note that, it is also possible to do this via the EntityManagerFactory.createEntityManager() method, which accepts properties. However, if you read the EclipseLink auditing example carefully, you'll notice that a shared connection pool (whose properties are derived from persistence.xml) is also created, and that actual connection in use would depend on whether you are performing reads or writes.

0

精彩评论

暂无评论...
验证码 换一张
取 消