JNDI,使用Persistence.xml,不使用用户/密码,仅使用从RMI独立获得的数据源

这个问题是Simple Application using JNDI and DataSource without Server,ServiceUnavailableException,ConnectException

的结果

我正在尝试通过jndi / rmi通过JPA使用数据源,而无需任何服务器。...

我有这个类/接口:

RemoteDataSource.java

public interface RemoteDataSource extends java.rmi.Remote {
    javax.sql.DataSource getDataSource() throws java.rmi.RemoteException;
}

ServerDataSource.java

public class ServerDataSource implements RemoteDataSource {

  private static java.rmi.registry.Registry registry;

  @Override
  public javax.sql.DataSource getDataSource() throws java.rmi.RemoteException {
    java.util.ResourceBundle credentials = java.util.ResourceBundle.getBundle("credentials");
    try {
      Class.forName("oracle.jdbc.pool.OracleDataSource");
      oracle.jdbc.pool.OracleDataSource ds = new oracle.jdbc.pool.OracleDataSource();
      ds.setURL(credentials.getString("URL"));
      ds.setUser(credentials.getString("username"));
      ds.setPassword(credentials.getString("PASSWORD"));
      return ds;
    } catch (Exception e) {
      e.printStackTrace();
      throw new java.rmi.RemoteException(e.getMessage());
    }
  }

  public static void main(String[] args) {
    try {
      registry = java.rmi.registry.LocateRegistry.createRegistry(1099);
      ServerDataSource outServerDataSource = new ServerDataSource();
      java.rmi.Remote remote = java.rmi.server.UnicastRemoteObject.exportObject(outServerDataSource,0);
      registry.bind("jdbc/extract",remote);
      System.out.println("registry: " + registry + " alive,listening on: " + 1099 + "!");
      while (true) {
        try {
          Thread.sleep(2000);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      System.out.println("Server shutdown!");
    }
  }
}

ClientDataSource.java

public class ClientDataSource {

  public static void main(String[] args) {
    try {
      java.rmi.registry.Registry registry = java.rmi.registry.LocateRegistry.getRegistry(null);
      RemoteDataSource remoteDataSource = (RemoteDataSource) registry.lookup("jdbc/extract");
      javax.sql.DataSource ds = (DataSource) remoteDataSource.getDataSource();
//      System.out.println("\nconnection: " + ds.getconnection());
      javax.persistence.EntityManagerFactory entityManagerFactory = javax.persistence.Persistence.createEntityManagerFactory("dBOracleTestPU");
      System.out.println("\nentityManagerFactory: " + entityManagerFactory + "\n");
      javax.persistence.EntityManager entityManager = entityManagerFactory.createEntityManager();
      System.out.println("\nentityManager: " + entityManager + "\n");
      try {
        Query query = entityManager.createNamedQuery("CatalogTemplates.findAll",CatalogTemplates.class);
        List<CatalogTemplates> listCatalogTemplates = query.getResultList();
      } catch (Exception e) {
        e.printStackTrace();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

persistence.xml RESOURCE_LOCAL version

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_1.xsd">
    <persistence-unit name="dBOracleTestPU" transaction-type="RESOURCE_LOCAL">
        <jta-data-source>jdbc/extract</jta-data-source>
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>my.package.CatalogTemplates</class>
    </persistence-unit>
</persistence>

persistence.xml JTA version http://www.adam-bien.com/roller/abien/entry/don_t_use_jpa_s

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_1.xsd">
    <persistence-unit name="dBOracleTestPU" transaction-type="JTA">
        <jta-data-source>jdbc/extracto</jta-data-source>
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>my.package.CatalogTemplates</class>
    </persistence-unit>
</persistence>

当我运行 ServerDataSource 的良好输出时:

registry: RegistryImpl[UnicastServerRef [liveRef: [endpoint:[82.0.164.211:1099](local),objID:[0:0:0,0]]]] alive,listening on: 1099!

但是,我在RESOURCE_LOCAL ClientDataSource 端获得了以下例外情况:

entityManagerFactory: org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl@4c178a76

[EL Warning]: transaction: 2019-12-30 09:57:08.664--ServerSession(1074389766)--PersistenceUnitInfo dBOracleTestPU has transactionType RESOURCE_LOCAL and therefore jtaDataSource will be ignored
[EL Info]: 2019-12-30 09:57:08.67--ServerSession(1074389766)--eclipseLink,version: eclipse Persistence Services - 2.7.5.v20191016-ea124dd158
[EL Severe]: ejb: 2019-12-30 09:57:08.673--ServerSession(1074389766)--Exception [eclipseLink-4021] (eclipse Persistence Services - 2.7.5.v20191016-ea124dd158): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Unable to acquire a connection from driver [null],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
javax.persistence.PersistenceException: Exception [eclipseLink-4021] (eclipse Persistence Services - 2.7.5.v20191016-ea124dd158): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Unable to acquire a connection from driver [null],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:857)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getabstractSession(EntityManagerFactoryDelegate.java:222)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:330)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:350)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:313)
    at mavenapplication.dboracletest.ClientDataSource.main(ClientDataSource.java:50)
Caused by: Exception [eclipseLink-4021] (eclipse Persistence Services - 2.7.5.v20191016-ea124dd158): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Unable to acquire a connection from driver [null],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:385)
    at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:93)
    at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:172)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource(DatabaseSessionImpl.java:225)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:809)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:256)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:772)
    ... 5 more

persistence.xml JTA version

entityManagerFactory: org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl@4c178a76

[EL Info]: 2019-12-30 10:32:27.926--ServerSession(769798433)--eclipseLink,version: eclipse Persistence Services - 2.7.5.v20191016-ea124dd158
[EL Severe]: ejb: 2019-12-30 10:32:27.97--ServerSession(769798433)--Exception [eclipseLink-7060] (eclipse Persistence Services - 2.7.5.v20191016-ea124dd158): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/extracto].
Internal Exception: javax.naming.NoInitialContextException: Need to specify class name in environment or system property,or as an applet parameter,or in an application resource file:  java.naming.factory.initial
javax.persistence.PersistenceException: Exception [eclipseLink-7060] (eclipse Persistence Services - 2.7.5.v20191016-ea124dd158): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/extracto].
Internal Exception: javax.naming.NoInitialContextException: Need to specify class name in environment or system property,or in an application resource file:  java.naming.factory.initial
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:857)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getabstractSession(EntityManagerFactoryDelegate.java:222)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:330)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:350)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:313)
    at mavenapplication.dboracletest.ClientDataSource.main(ClientDataSource.java:50)
Caused by: Exception [eclipseLink-7060] (eclipse Persistence Services - 2.7.5.v20191016-ea124dd158): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/extracto].
Internal Exception: javax.naming.NoInitialContextException: Need to specify class name in environment or system property,or in an application resource file:  java.naming.factory.initial
    at org.eclipse.persistence.exceptions.ValidationException.cannotAcquireDataSource(ValidationException.java:525)
    at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:124)
    at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:172)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource(DatabaseSessionImpl.java:225)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:809)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:256)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:772)
    ... 5 more
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property,or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:410)
    at javax.naming.InitialContext.lookup(InitialContext.java:421)
    at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:118)
    ... 10 more

是否可以在persistence.xml上使用不带属性文件的数据源(指示客户端/密码)

qe2004 回答:JNDI,使用Persistence.xml,不使用用户/密码,仅使用从RMI独立获得的数据源

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2838891.html

大家都在问