简体   繁体   English

(OpenJPA / WAS)如何对容器管理的实体管理器使用容器管理的身份验证

[英](OpenJPA/WAS) How to use the Container Managed Authentication for the Container Managed Entity Manager

Objective 目的

To understand how to configure WAS or OpenJPA to use the Container Managed Authentication with the Container Managed Entity Manager. 了解如何将WAS或OpenJPA配置为与容器管理的实体管理器一起使用容器管理的身份验证。

Issue 问题

Trying to access the Oracle database via the JDBC Datasource registered in JNDI as "jdbc/DB2" (it refers to Oracle) defined in persistence.xml. 尝试通过在JNDI中注册为在persistence.xml中定义的“ jdbc / DB2”(指Oracle)的JDBC数据源访问Oracle数据库。

persistence.xml persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="DB2AccountUnit" transaction-type="JTA">
        <jta-data-source>jdbc/DB2</jta-data-source>
        <class>sample.jpa.Db2account</class>
        <properties>
            <property name="openjpa.Log" value="File=C:/opt/openjpa/org.apache.openjpa.log, DefaultLevel=INFO, Runtime=TRACE, Tool=INFO, SQL=TRACE, JDBC=TRACE" />
            <!-- Without these properties, the error is caused.
            <property name="openjpa.ConnectionUserName" value="User" />
            <property name="openjpa.ConnectionPassword" value="Password" />
             -->
        </properties>
    </persistence-unit>
</persistence>

However when the container managed entity manager for the persistence unit is accessed, ORA-01017 invalid usrname/password is thrown. 但是,当访问持久性单元的容器管理的实体管理器时,将抛出ORA-01017无效的usrname / password。 If ConnectionUserName/ConnectionPassword are specified in the persistence.xml, the error is resolved. 如果在persistence.xml中指定了ConnectionUserName / ConnectionPassword,则错误将得到解决。

Error 错误

[26/09/15 20:56:31:584 AEST] 00000090 SystemErr     
R <openjpa-2.2.3-SNAPSHOT-r422266:1686911 fatal general error> org.apache.openjpa.persistence.PersistenceException: 
ORA-01017: invalid username/password; logon denied
DSRA0010E: SQL State = 72000, Error Code = 1,017
[26/09/15 20:56:31:585 AEST] 00000090 SystemErr     R   at org.apache.openjpa.jdbc.sql.DBDictionaryFactory.newDBDictionary(DBDictionaryFactory.java:102)
...
[26/09/15 20:56:31:601 AEST] 00000090 SystemErr     R   at sample.jpa.servlet.CreateAccount.doPost(CreateAccount.java:23)

WAS

  • JAAS is associated with the Datasource to use the Continaer Managed Authentication JAAS与数据源关联以使用Continaer托管身份验证
  • From WAS console JDBC Datasource, Test connection to the Datasource succeeds. 从WAS控制台JDBC数据源,成功测试到数据源的连接。
  • The namespace dump verifies "jdbc/DB2" is registed in the server1 namespace. 名称空间转储将验证“ jdbc / DB2”已在server1名称空间中注册。
  • WAS is not ND and server was restarted several times. WAS不是ND,并且服务器已多次重启。

在此处输入图片说明

Servlet using the Entity Manager Servlet使用实体管理器
The original is from IBM developerWorks article. 原始内容来自IBM developerWorks文章。 Injecting the entity manager in the servlet instance variable is not multi thread safe but for the sake of testing. 将实体管理器注入servlet实例变量不是多线程安全的,而是出于测试目的。

@WebServlet("/CreateAccount")
public class CreateAccount extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    private static final long serialVersionUID = 1L;

    @PersistenceContext(unitName="DB2AccountUnit")
    EntityManager em;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    synchronized protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        InitialContext context = null;
        UserTransaction ut = null;

        try {
            context = new InitialContext();
            ut = (UserTransaction)context.lookup("java:comp/UserTransaction");

            if(ut == null){
                System.out.println("UserTransactin is NULL");
            }

            //em.getTransaction().begin();
            ut.begin();

            long accno = Long.parseLong(request.getParameter("ACCNO"));
            Db2account account1 = em.find(Db2account.class, accno); // <----- Error occurs here.

            if (account1 != null) {
                throw new AccountException("Error : sample.jpa.servlet.CreateAccount : Account Number already exists:" + accno);
            } else {
                SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
                java.sql.Date dbDate = new java.sql.Date(format.parse(request.getParameter("DOP"), new ParsePosition(0)).getTime());

                Db2account account = new Db2account();
                account.setOwner(request.getParameter("OWNERNAME"));
                account.setAccno(accno);
                account.setAccountType(request.getParameter("ACCOUNTTYPE"));
                account.setBalance(new BigDecimal(request.getParameter("BALANCE")));
                account.setDateOpen(dbDate);

                em.persist(account);
                // em.getTransaction().commit();
                ut.commit();
                response.sendRedirect("Create_Account.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
            //em.getTransaction().rollback();
            try{
                ut.rollback();
            } catch (SystemException se){
                se.printStackTrace();
            }
        }
    }
}

Question

Please help understanding why the JAAS is not used and how I can configure WAS or JPA to use it. 请帮助理解为什么不使用JAAS,以及如何配置WAS或JPA以使用它。

Environment 环境

IBM WAS 8.5.5.7 for Developers (Apache openjpa-2.2.3-SNAPSHOT-r422266) 适用于开发人员的IBM WAS 8.5.5.7(Apache openjpa-2.2.3-SNAPSHOT-r422266)
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production Oracle Database 11g企业版11.2.0.1.0版-64位生产
Windows 7 64 bit on DELL Latitude core i7 2.8GHz 8G memory & SSD HDD DELL Latitude酷睿i7 2.8GHz 8G内存和SSD HDD上的Windows 7 64位

Log 日志记录

Oracle error at the application start 应用程序启动时出现Oracle错误

[26/09/15 20:39:59:860 AEST] 0000008e ApplicationMg A   WSVR0200I: Starting application: Account
[26/09/15 20:40:00:062 AEST] 0000008e driver        E   CE9D1D8D Throwing SQLException: ORA-01017: invalid username/password; logon denied
1017
[26/09/15 20:40:00:065 AEST] 0000008e driver        E   DABC15C4 Throwing SQLException: 430
[26/09/15 20:40:00:089 AEST] 0000008e Runtime       W   CWWJP9991W: openjpa.Runtime: Warn: An error occurred while registering a ClassTransformer with JPAPUnitInfo(PuId=Account#lib/AccountDB2JPA.jar#DB2AccountUnit_HOGE)@504fa90. The error has been consumed. To see it, set your openjpa.Runtime log level to TRACE. Load-time class transformation will not be available.
[26/09/15 20:40:00:188 AEST] 0000008e driver        E   33A50E58 Throwing SQLException: ORA-01017: invalid username/password; logon denied
1017
[26/09/15 20:40:00:190 AEST] 0000008e driver        E   C5B591D4 Throwing SQLException: 430
[26/09/15 20:40:00:206 AEST] 0000008e Runtime       W   CWWJP9991W: openjpa.Runtime: Warn: An error occurred while registering a ClassTransformer with JPAPUnitInfo(PuId=Account#AccountWeb.war#DB2AccountUnit_HOGE)@4167230b. The error has been consumed. To see it, set your openjpa.Runtime log level to TRACE. Load-time class transformation will not be available.
[26/09/15 20:40:00:315 AEST] 0000008e driver        E   FDD70E84 Throwing SQLException: ORA-01017: invalid username/password; logon denied
1017
[26/09/15 20:40:00:317 AEST] 0000008e driver        E   CB5DFD5C Throwing SQLException: 430
[26/09/15 20:40:00:331 AEST] 0000008e Runtime       W   CWWJP9991W: openjpa.Runtime: Warn: An error occurred while registering a ClassTransformer with JPAPUnitInfo(PuId=Account#AccountWeb.war#DerbyAccountUnit)@e9b08d89. The error has been consumed. To see it, set your openjpa.Runtime log level to TRACE. Load-time class transformation will not be available.
[26/09/15 20:40:00:333 AEST] 0000008e Runtime       I   CWWJP9990I: openjpa.Runtime: Info: OpenJPA dynamically loaded a validation provider.
[26/09/15 20:40:00:420 AEST] 0000008e WASSessionCor I SessionContextRegistry getSessionContext SESN0176I: Will create a new session context for application key default_host/AccountWeb
[26/09/15 20:40:00:470 AEST] 0000008e ApplicationMg A   WSVR0221I: Application started: Account

Oracle error at the execution time of CreateAccount 执行CreateAccount时发生Oracle错误

[26/09/15 20:56:31:584 AEST] 00000090 SystemErr     R <openjpa-2.2.3-SNAPSHOT-r422266:1686911 fatal general error> org.apache.openjpa.persistence.PersistenceException: ORA-01017: invalid username/password; logon denied
 DSRA0010E: SQL State = 72000, Error Code = 1,017
[26/09/15 20:56:31:585 AEST] 00000090 SystemErr     R   at org.apache.openjpa.jdbc.sql.DBDictionaryFactory.newDBDictionary(DBDictionaryFactory.java:102)
[26/09/15 20:56:31:585 AEST] 00000090 SystemErr     R   at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getDBDictionaryInstance(JDBCConfigurationImpl.java:603)
[26/09/15 20:56:31:586 AEST] 00000090 SystemErr     R   at org.apache.openjpa.jdbc.meta.MappingRepository.endConfiguration(MappingRepository.java:1518)
[26/09/15 20:56:31:586 AEST] 00000090 SystemErr     R   at org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:531)
[26/09/15 20:56:31:593 AEST] 00000090 SystemErr     R   at org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:456)
[26/09/15 20:56:31:594 AEST] 00000090 SystemErr     R   at org.apache.openjpa.lib.conf.PluginValue.instantiate(PluginValue.java:121)
[26/09/15 20:56:31:594 AEST] 00000090 SystemErr     R   at org.apache.openjpa.conf.MetaDataRepositoryValue.instantiate(MetaDataRepositoryValue.java:68)
[26/09/15 20:56:31:595 AEST] 00000090 SystemErr     R   at org.apache.openjpa.lib.conf.ObjectValue.instantiate(ObjectValue.java:83)
[26/09/15 20:56:31:595 AEST] 00000090 SystemErr     R   at org.apache.openjpa.conf.OpenJPAConfigurationImpl.newMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:967)
[26/09/15 20:56:31:596 AEST] 00000090 SystemErr     R   at org.apache.openjpa.conf.OpenJPAConfigurationImpl.getMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:958)
[26/09/15 20:56:31:596 AEST] 00000090 SystemErr     R   at org.apache.openjpa.kernel.AbstractBrokerFactory.makeReadOnly(AbstractBrokerFactory.java:642)
[26/09/15 20:56:31:597 AEST] 00000090 SystemErr     R   at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:203)
[26/09/15 20:56:31:597 AEST] 00000090 SystemErr     R   at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:156)
[26/09/15 20:56:31:597 AEST] 00000090 SystemErr     R   at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:228)
[26/09/15 20:56:31:598 AEST] 00000090 SystemErr     R   at com.ibm.ws.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:72)
[26/09/15 20:56:31:598 AEST] 00000090 SystemErr     R   at com.ibm.ws.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:35)
[26/09/15 20:56:31:599 AEST] 00000090 SystemErr     R   at com.ibm.ws.jpa.management.JPAEMPool.getEntityManager(JPAEMPool.java:170)
[26/09/15 20:56:31:599 AEST] 00000090 SystemErr     R   at com.ibm.ws.jpa.management.JPATxEntityManager.getEMInvocationInfo(JPATxEntityManager.java:259)
[26/09/15 20:56:31:600 AEST] 00000090 SystemErr     R   at com.ibm.ws.jpa.management.JPATxEntityManager.getEMInvocationInfo(JPATxEntityManager.java:191)
[26/09/15 20:56:31:600 AEST] 00000090 SystemErr     R   at com.ibm.ws.jpa.management.JPAEntityManager.getTransaction(JPAEntityManager.java:421)
[26/09/15 20:56:31:601 AEST] 00000090 SystemErr     R   at sample.jpa.servlet.CreateAccount.doPost(CreateAccount.java:23)
[26/09/15 20:56:31:601 AEST] 00000090 SystemErr     R   at javax.servlet.http.HttpServlet.service(HttpServlet.java:595)
[26/09/15 20:56:31:602 AEST] 00000090 SystemErr     R   at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
[26/09/15 20:56:31:602 AEST] 00000090 SystemErr     R   at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1232)
[26/09/15 20:56:31:602 AEST] 00000090 SystemErr     R   at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:781)
[26/09/15 20:56:31:602 AEST] 00000090 SystemErr     R   at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:480)
[26/09/15 20:56:31:603 AEST] 00000090 SystemErr     R   at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
[26/09/15 20:56:31:605 AEST] 00000090 SystemErr     R   at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1114)
[26/09/15 20:56:31:605 AEST] 00000090 SystemErr     R   at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3926)
[26/09/15 20:56:31:605 AEST] 00000090 SystemErr     R   at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
[26/09/15 20:56:31:605 AEST] 00000090 SystemErr     R   at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1007)
[26/09/15 20:56:31:605 AEST] 00000090 SystemErr     R   at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1817)
[26/09/15 20:56:31:606 AEST] 00000090 SystemErr     R   at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)
[26/09/15 20:56:31:606 AEST] 00000090 SystemErr     R   at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:463)
[26/09/15 20:56:31:606 AEST] 00000090 SystemErr     R   at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:530)
[26/09/15 20:56:31:606 AEST] 00000090 SystemErr     R   at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:316)
[26/09/15 20:56:31:606 AEST] 00000090 SystemErr     R   at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:287)
[26/09/15 20:56:31:607 AEST] 00000090 SystemErr     R   at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
[26/09/15 20:56:31:607 AEST] 00000090 SystemErr     R   at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
[26/09/15 20:56:31:607 AEST] 00000090 SystemErr     R   at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
[26/09/15 20:56:31:607 AEST] 00000090 SystemErr     R   at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
[26/09/15 20:56:31:607 AEST] 00000090 SystemErr     R   at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
[26/09/15 20:56:31:607 AEST] 00000090 SystemErr     R   at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
[26/09/15 20:56:31:608 AEST] 00000090 SystemErr     R   at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
[26/09/15 20:56:31:608 AEST] 00000090 SystemErr     R   at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
[26/09/15 20:56:31:608 AEST] 00000090 SystemErr     R   at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
[26/09/15 20:56:31:608 AEST] 00000090 SystemErr     R   at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1881)
[26/09/15 20:56:31:609 AEST] 00000090 SystemErr     R Caused by: java.sql.SQLException: ORA-01017: invalid username/password; logon denied
 DSRA0010E: SQL State = 72000, Error Code = 1,017
[26/09/15 20:56:31:609 AEST] 00000090 SystemErr     R   at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:459)
[26/09/15 20:56:31:610 AEST] 00000090 SystemErr     R   at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:392)
[26/09/15 20:56:31:610 AEST] 00000090 SystemErr     R   at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:384)
[26/09/15 20:56:31:611 AEST] 00000090 SystemErr     R   at oracle.jdbc.driver.T4CTTIfun.processError(T4CTTIfun.java:767)
[26/09/15 20:56:31:611 AEST] 00000090 SystemErr     R   at oracle.jdbc.driver.T4CTTIoauthenticate.processError(T4CTTIoauthenticate.java:450)
[26/09/15 20:56:31:612 AEST] 00000090 SystemErr     R   at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:539)
[26/09/15 20:56:31:612 AEST] 00000090 SystemErr     R   at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:235)
[26/09/15 20:56:31:612 AEST] 00000090 SystemErr     R   at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:382)
[26/09/15 20:56:31:612 AEST] 00000090 SystemErr     R   at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:822)
[26/09/15 20:56:31:612 AEST] 00000090 SystemErr     R   at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:477)
[26/09/15 20:56:31:613 AEST] 00000090 SystemErr     R   at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:564)
[26/09/15 20:56:31:613 AEST] 00000090 SystemErr     R   at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:251)
[26/09/15 20:56:31:613 AEST] 00000090 SystemErr     R   at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:29)
[26/09/15 20:56:31:613 AEST] 00000090 SystemErr     R   at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:563)
[26/09/15 20:56:31:613 AEST] 00000090 SystemErr     R   at oracle.jdbc.pool.OracleDataSource.getPhysicalConnection(OracleDataSource.java:454)
[26/09/15 20:56:31:613 AEST] 00000090 SystemErr     R   at oracle.jdbc.pool.OracleDataSource.getConnection(OracleDataSource.java:328)
[26/09/15 20:56:31:614 AEST] 00000090 SystemErr     R   at oracle.jdbc.pool.OracleConnectionPoolDataSource.getPhysicalConnection(OracleConnectionPoolDataSource.java:379)
[26/09/15 20:56:31:614 AEST] 00000090 SystemErr     R   at oracle.jdbc.pool.OracleConnectionPoolDataSource.getPooledConnection(OracleConnectionPoolDataSource.java:165)
[26/09/15 20:56:31:614 AEST] 00000090 SystemErr     R   at oracle.jdbc.pool.OracleConnectionPoolDataSource.getPooledConnection(OracleConnectionPoolDataSource.java:111)
[26/09/15 20:56:31:614 AEST] 00000090 SystemErr     R   at com.ibm.ws.rsadapter.spi.InternalGenericDataStoreHelper$1.run(InternalGenericDataStoreHelper.java:1365)
[26/09/15 20:56:31:615 AEST] 00000090 SystemErr     R   at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
[26/09/15 20:56:31:615 AEST] 00000090 SystemErr     R   at com.ibm.ws.rsadapter.spi.InternalGenericDataStoreHelper.getPooledConnection(InternalGenericDataStoreHelper.java:1384)
[26/09/15 20:56:31:615 AEST] 00000090 SystemErr     R   at com.ibm.ws.rsadapter.spi.WSRdbDataSource.getPooledConnection(WSRdbDataSource.java:2150)
[26/09/15 20:56:31:615 AEST] 00000090 SystemErr     R   at com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl.getConnection(WSManagedConnectionFactoryImpl.java:1787)
[26/09/15 20:56:31:615 AEST] 00000090 SystemErr     R   at com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl.createManagedConnection(WSManagedConnectionFactoryImpl.java:1557)
[26/09/15 20:56:31:616 AEST] 00000090 SystemErr     R   at com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl.createManagedConnection(WSManagedConnectionFactoryImpl.java:1113)
[26/09/15 20:56:31:616 AEST] 00000090 SystemErr     R   at com.ibm.ejs.j2c.FreePool.createManagedConnectionWithMCWrapper(FreePool.java:2161)
[26/09/15 20:56:31:616 AEST] 00000090 SystemErr     R   at com.ibm.ejs.j2c.FreePool.createOrWaitForConnection(FreePool.java:1839)
[26/09/15 20:56:31:616 AEST] 00000090 SystemErr     R   at com.ibm.ejs.j2c.PoolManager.reserve(PoolManager.java:3818)
[26/09/15 20:56:31:616 AEST] 00000090 SystemErr     R   at com.ibm.ejs.j2c.PoolManager.reserve(PoolManager.java:3094)
[26/09/15 20:56:31:617 AEST] 00000090 SystemErr     R   at com.ibm.ejs.j2c.ConnectionManager.allocateMCWrapper(ConnectionManager.java:1548)
[26/09/15 20:56:31:617 AEST] 00000090 SystemErr     R   at com.ibm.ejs.j2c.ConnectionManager.allocateConnection(ConnectionManager.java:1031)
[26/09/15 20:56:31:617 AEST] 00000090 SystemErr     R   at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:644)
[26/09/15 20:56:31:618 AEST] 00000090 SystemErr     R   at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:611)
[26/09/15 20:56:31:618 AEST] 00000090 SystemErr     R   at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getContainerManagedConnection(WSJdbcDataSource.java:958)
[26/09/15 20:56:31:619 AEST] 00000090 SystemErr     R   at com.ibm.ws.jpa.management.JPACMDataSource.getConnection(JPACMDataSource.java:44)
[26/09/15 20:56:31:619 AEST] 00000090 SystemErr     R   at org.apache.openjpa.lib.jdbc.DelegatingDataSource.getConnection(DelegatingDataSource.java:110)
[26/09/15 20:56:31:619 AEST] 00000090 SystemErr     R   at org.apache.openjpa.lib.jdbc.DecoratingDataSource.getConnection(DecoratingDataSource.java:87)
[26/09/15 20:56:31:620 AEST] 00000090 SystemErr     R   at org.apache.openjpa.jdbc.sql.DBDictionaryFactory.newDBDictionary(DBDictionaryFactory.java:91)
[26/09/15 20:56:31:620 AEST] 00000090 SystemErr     R   ... 46 more

JNDI Namespace (server1) JNDI命名空间(服务器1)

==============================================================================
Name Space Dump
   Context factory: com.ibm.websphere.naming.WsnInitialContextFactory
   Provider URL: corbaloc:iiop:localhost:2814
   Requested root context: cell
   Starting context: (top)=ISVNKDLT0000926Node01Cell
   Formatting rules: jndi
   Time of dump: Sat Sep 26 21:08:19 AEST 2015
==============================================================================
80 (top)/nodes/ISVNKDLT0000926Node02/servers/server1/jdbc/DB2
80                                                    javax.resource.cci.ConnectionFactory

OpenJPA Log at Build Time Enhancement 构建时的OpenJPA日志增强

7  DB2AccountUnit_HOGE  TRACE  [main] openjpa.Runtime - Setting the following properties from "file:/D:/Home/Workspaces/WAS/AccountDB2JPA/src/META-INF/persistence.xml" into configuration: {openjpa.ConnectionFactoryMode=managed, openjpa.ConnectionFactoryName=jdbc/DB2, openjpa.TransactionMode=managed, openjpa.MetaDataFactory=jpa(Types=sample.jpa.Db2account), openjpa.Log=File=C:/opt/openjpa/org.apache.openjpa.log, DefaultLevel=INFO, Runtime=TRACE, Tool=INFO, SQL=TRACE, JDBC=TRACE, PersistenceVersion=1.0, openjpa.Id=DB2AccountUnit_HOGE}
14  DB2AccountUnit_HOGE  TRACE  [main] openjpa.Runtime - No cache marshaller found for id org.apache.openjpa.conf.MetaDataCacheMaintenance.
55  DB2AccountUnit_HOGE  INFO   [main] openjpa.Tool - Enhancer running on type "sample.jpa.Db2account".

References 参考文献


Update 更新资料

Tried the JPA example of IBM redbook WebSphere Application Server for Developers V7 . 尝试了IBM红皮书WebSphere Application Server for Developers V7的JPA示例。 Deployed the application version 2 and got the same error. 部署了应用程序版本2,并得到了相同的错误。

In the persistence.xml you have to use resource ref eg java:comp/env/jdbc/DB2Ref instead of global JNDI name. persistence.xml您必须使用资源引用,例如java:comp/env/jdbc/DB2Ref而不是全局JNDI名称。 And you have to define resource reference either in deployment descriptor or via annotation with name jdbc/DB2Ref in the module/component that is using JPA. 而且,您必须在部署描述符中或通过使用JPA的模块/组件中名称为jdbc/DB2Ref注释来定义资源引用。

If you want to use global JNDI name in the persistnece.xml, then you have to set Component-managed authentication alias not the Container-managed , container is used only when access to datasource is via reference. 如果要在persistnece.xml中使用全局JNDI名称,则必须设置Component-managed authentication alias而不是Container-managed ,仅当通过引用访问数据源时才使用容器。

Based on the answer by @Gas, did some experiments. 根据@Gas的答案,进行了一些实验。

@Resource in Servlet and Global JNDI in persistence.xml Servlet中的@Resource和persistence.xml中的Global JNDI

Only container managed authentication/JAAS specified in the Datasource setting in IBM WAS. IBM WAS的“数据源”设置中仅指定了容器管理的认证/ JAAS。
Fail 失败

[Servlet]
@WebServlet("/CreateAccount")
public class CreateAccount extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    static final long serialVersionUID = 1L;
    @Resource(name="jdbc/DB2")
    private DataSource ds;

[persistence.xml]
<persistence-unit name="DB2AccountUnitLocal" transaction-type="RESOURCE_LOCAL">
    <jta-data-source>jdbc/DB2</jta-data-source>
    <class>sample.jpa.Db2account</class>
</persistence-unit>

Result 结果

[28/09/15 14:35:55:419 AEST] 000000f2 webapp        E com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E: [Servlet Error]-[sample.jpa.servlet.CreateAccount]: <openjpa-2.2.3-SNAPSHOT-r422266:1686911 fatal general error> org.apache.openjpa.persistence.PersistenceException: ORA-01017: invalid username/password; logon denied
 DSRA0010E: SQL State = 72000, Error Code = 1,017

@Resource in Servlet and ENC JNDI in persistence.xml Servlet中的@Resource和persistence.xml中的ENC JNDI

Only container managed authentication/JAAS specified in the Datasource setting in IBM WAS. IBM WAS的“数据源”设置中仅指定了容器管理的认证/ JAAS。
Success 成功

[Servlet]
@WebServlet("/CreateAccount")
public class CreateAccount extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    static final long serialVersionUID = 1L;
    @Resource(name="jdbc/DB2")
    private DataSource ds;

[persistence.xml]
<persistence-unit name="DB2AccountUnitLocal" transaction-type="RESOURCE_LOCAL">
    <jta-data-source>java:comp/env/jdbc/DB2</jta-data-source>
    <class>sample.jpa.Db2account</class>
</persistence-unit>

JDBC resource-ref in web.xml and ENC JNDI in persistence.xml web.xml中的JDBC资源引用和persistence.xml中的ENC JNDI

Only container managed authentication/JAAS specified in the Datasource setting in IBM WAS. IBM WAS的“数据源”设置中仅指定了容器管理的认证/ JAAS。
Success 成功

[Servlet]
@WebServlet("/CreateAccount")
public class CreateAccount extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    static final long serialVersionUID = 1L;
    //@Resource(name="jdbc/DB2")
    //private DataSource ds;

[web.xml]
<resource-ref>
  <res-ref-name>jdbc/DB2</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>

[persistence.xml]
<persistence-unit name="DB2AccountUnitLocal" transaction-type="RESOURCE_LOCAL">
    <jta-data-source>java:comp/env/jdbc/DB2</jta-data-source>
    <class>sample.jpa.Db2account</class>
</persistence-unit>

Component Managed Authentication 组件管理的身份验证

Only component-managed authentication alias. 仅组件管理的身份验证别名。 Mapping-configuration alias is DefaultPrincipalMapping. 映射配置别名为DefaultPrincipalMapping。
Success 成功

在此处输入图片说明

[Servlet]
@WebServlet("/CreateAccount")
public class CreateAccount extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    static final long serialVersionUID = 1L;
    //@Resource(name="jdbc/DB2")
    //private DataSource ds;

[web.xml]
  <resource-ref>
  <res-ref-name>jdbc/DB2</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
 </resource-ref>

[persistence.xml]
<persistence-unit name="DB2AccountUnitLocal" transaction-type="RESOURCE_LOCAL">
    <non-jta-data-source>jdbc/DB2</non-jta-data-source>
    <class>sample.jpa.Db2account</class>
</persistence-unit>

The JPA 2.0 specification says it is global JNDI name of the datasource to be specified. JPA 2.0规范说这是要指定的数据源的全局JNDI名称。 IBM WAS Knowledge Center says that ENC name is WAS extention. IBM WAS知识中心说,ENC名称是WAS扩展。

I suppose that as the container managed authentication is via resource reference and EJB3 @Resource(name) is ENC name resource reference java:comp/env/, using container authentication for JPA is probaby vendor specific. 我猜想由于容器管理的身份验证是通过资源引用进行的,而EJB3 @Resource(name)是ENC名称资源引用java:comp / env /,因此对JPA使用容器身份验证是特定于供应商的。

JSR 317: JavaTM Persistence API, Version 2.0 Final JSR 317:JavaTM持久性API,版本2.0最终版

8.2.1.5 jta-data-source, non-jta-data-source 8.2.1.5 jta数据源,非jta数据源

In Java EE environments, the jta-data-source and non-jta-data-source elements are used to specify the global JNDI name of the JTA and/or non-JTA data source to be used by the persistence provider. 在Java EE环境中,jta-data-source和non-jta-data-source元素用于指定持久性提供程序要使用的JTA和/或非JTA数据源全局JNDI名称 If neither is specified, the deployer must specify a JTA data source at deployment or a 如果未指定,则部署者必须在部署时指定JTA数据源或

IBM WAS 8.5.5 Associating persistence providers and data sources IBM WAS 8.5.5关联持久性提供程序和数据源

The JPA for WebSphere Application Server solution extends the JNDI data-source implementation to allow you to reference data sources in the component name space . JPA for WebSphere Application Server解决方案扩展了JNDI数据源实现,以允许您引用组件名称空间中的数据源 In the EJB or web module deployment descriptor file, this is the element. 在EJB或Web模块部署描述符文件中,这是元素。

You can prefix the data source with java:comp/env/ so the application indirectly references the data source by using the local JNDI name. 您可以使用java:comp / env /为数据源添加前缀,以便应用程序使用本地JNDI名称间接引用数据源。


在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM