1

I am trying to create EntityManager at runtime so I can set different user name and password from a config.xml

Here is the code to to create the EntityManager

map.put(PersistenceUnitProperties.JDBC_USER,  SystemConfig.getConfig().getString("jdbcUser"));
map.put(PersistenceUnitProperties.JDBC_PASSWORD, SystemConfig.getConfig().getString("jdbcPassword"));
map.put(PersistenceUnitProperties.JDBC_URL, SystemConfig.getConfig().getString("jdbcUrl"));
map.put(PersistenceUnitProperties.JDBC_DRIVER, "net.sourceforge.jtds.jdbc.Driver");
EntityManager eMgr = Persistence.createEntityManagerFactory("myPU", map).createEntityManager();

My program structure in unix: ~/test/myProgram.jar ~/test/lib/*.jar (all library and jdbc.jar here)

The program works fine when I run the program inside the folder /test/ However when I run the program under the root ~/ I get the Exception [EclipseLink-4021]. Any idea why??

Note: Ultimately I need to schedule the program using cron job.

[EL Info]: 2013-05-06 12:10:18.989--ServerSession(1729331687)--EclipseLink, version: Eclipse Persistence Services - 2.4.1.v20121003-ad44345
[EL Severe]: ejb: 2013-05-06 12:10:19.002--ServerSession(1729331687)--Exception [EclipseLink-4021] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Unable to acquire a connection from driver [net.sourceforge.jtds.jdbc.Driver], user [null] and URL [null].  Verify that you have set the expected driver class and URL.  Check your login, persistence.xml or sessions.xml resource.  The jdbc.driver property should be set to a class that is compatible with your database platform
Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-4021] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Unable to acquire a connection from driver [net.sourceforge.jtds.jdbc.Driver], user [null] and URL [null].  Verify that you have set the expected driver class and URL.  Check your login, persistence.xml or sessions.xml resource.  The jdbc.driver property should be set to a class that is compatible with your database platform
        at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:602)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:186)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:278)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:304)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:282)
        at dcsmailcheck.MailResend.<init>(MailResend.java:61)
        at dcsmailcheck.Main.main(Main.java:49)
Caused by: Exception [EclipseLink-4021] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Unable to acquire a connection from driver [net.sourceforge.jtds.jdbc.Driver], user [null] and URL [null].  Verify that you have set the expected driver class and URL.  Check your login, persistence.xml or sessions.xml resource.  The jdbc.driver property should be set to a class that is compatible with your database platform
        at org.eclipse.persistence.exceptions.DatabaseException.unableToAcquireConnectionFromDriverException(DatabaseException.java:376)
        at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:91)
        at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
        at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:685)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:215)
        at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:542)
        ... 6 more
4

1 回答 1

0

由于您的代码在文件夹 ~/test 中工作,因此似乎SystemConfig.getConfig()无法从该位置加载属性。确保已将属性的路径添加到 CLASSPATH。

于 2013-05-06T05:05:20.343 回答