(OpenJPA / WAS)如何为Container Managed Entity Manager使用Container Managed Authentication

时间:2015-09-26 22:17:40

标签: jpa jdbc websphere jaas container-managed

目的

了解如何配置WAS或OpenJPA以使用Container Managed Authentication与Container Managed Entity Manager。

问题

尝试通过在JNDI中注册的JDBC数据源以persistence.xml中定义的“jdbc / DB2”(它指的是Oracle)来访问Oracle数据库。

的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>

但是,当访问持久性单元的容器管理实体管理器时,会抛出ORA-01017无效的usrname / password。如果在persistence.xml中指定了ConnectionUserName / ConnectionPassword,则会解决错误。

错误

[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与数据源关联以使用Continaer托管身份验证
  • 从WAS控制台JDBC数据源,到数据源的测试连接成功。
  • 命名空间转储验证“jdbc / DB2”是否在server1命名空间中注册。
  • WAS不是ND,服务器重启了好几次。

enter image description here

使用实体管理器的Servlet
原文来自IBM developerWorks文章。在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();
            }
        }
    }
}

问题

请帮助理解为什么不使用JAAS以及如何配置WAS或JPA来使用它。

环境

IBM WAS 8.5.5.7 for Developers(Apache openjpa-2.2.3-SNAPSHOT-r422266)
Oracle Database 11g企业版11.2.0.1.0版 - 64位生产
DELL Latitude核心i7 2.8GHz 8G内存和Windows上的Windows 7 64位SSD HDD

日志

应用程序启动时的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

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命名空间(server1)

==============================================================================
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日志构建时间增强

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".

参考

更新

尝试了IBM redbook WebSphere Application Server for Developers V7的JPA示例。部署了应用程序版本2并得到了同样的错误。

2 个答案:

答案 0 :(得分:1)

persistence.xml中您必须使用资源引用,例如java:comp/env/jdbc/DB2Ref而不是全局JNDI名称。您必须在部署描述符中或通过使用JPA的模块/组件中名称为jdbc/DB2Ref的注释定义资源引用。

如果要在persistnece.xml中使用全局JNDI名称,则必须设置Component-managed authentication alias而不是Container-managed,仅当通过引用访问数据源时才使用容器。

答案 1 :(得分:0)

基于@Gas的回答,做了一些实验。

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

仅在IBM WAS的“数据源”设置中指定了容器管理的身份验证/ JAAS 的失败

[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>

结果

[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

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

仅在IBM WAS的“数据源”设置中指定了容器管理的身份验证/ JAAS <强>成功

[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>

web.xml中的JDBC resource-ref和persistence.xml中的ENC JNDI

仅在IBM WAS的“数据源”设置中指定了容器管理的身份验证/ JAAS <强>成功

[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>

组件管理身份验证

仅限组件管理的身份验证别名。 Mapping-configuration别名是DefaultPrincipalMapping <强>成功

enter image description here

[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>

JPA 2.0规范说它是要指定的数据源的全局JNDI名称。 IBM WAS知识中心表示ENC名称是WAS扩展名。

我认为由于容器管理身份验证是通过资源引用而EJB3 @Resource(name)是ENC名称资源引用java:comp / env /,因此使用JPA的容器身份验证是probaby供应商特定的。

JSR 317:JavaTM持久性API,2.0版最终版

  

8.2.1.5 jta-data-source,non-jta-data-source

     

在Java EE环境中,jta-data-source和non-jta-data-source元素用于指定JTA和/或非JTA数据源的 全局JNDI名称 供持久性提供程序使用。如果两者都未指定,则部署者必须在部署时指定JTA数据源或

IBM WAS 8.5.5 Associating persistence providers and data sources

  

用于WebSphere Application Server解决方案的JPA 扩展了JNDI数据源实现,以允许您引用组件名称空间中的数据源。在EJB或Web模块部署描述符文件中,这是元素。

     

您可以使用java:comp / env /为数据源添加前缀,以便应用程序使用本地JNDI名称间接引用数据源。

enter image description here