WebSphere9上的Hibernate间接JNDI绑定

WebSphere9上的Hibernate间接JNDI绑定,hibernate,binding,websphere,jndi,Hibernate,Binding,Websphere,Jndi,我对websphere上的datasource间接jndi绑定有问题 我在我的web.xml中添加了以下资源: <resource-ref> <description>Data Source JNDI</description> <res-ref-name>jdbc/RESOURCE_L</res-ref-name> <res-type>javax.sql.DataSource</res-typ

我对websphere上的datasource间接jndi绑定有问题

我在我的web.xml中添加了以下资源:

<resource-ref>
    <description>Data Source JNDI</description>
    <res-ref-name>jdbc/RESOURCE_L</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
</resource-ref> 
这个应用程序似乎运行良好

我想了解为什么会有这种例外,以及如何解决这个问题。 有什么想法吗


非常感谢您,mutch

您看到的是,Hibernate在尝试使用GenericConnection时抛出了一个异常,GenericConnection是java.sql.Connection的WebSphere内部模型。您可以看到,当WebSphere启动应用程序时,它必须在应用程序启动过程中尽早注册类转换器,因为一旦应用程序类加载器加载了一个类,它就不再有资格进行进一步的转换(JPA持久性提供程序可以增强支持持久性的类,并添加内部操作所需的其他方法和字段)。类转换器只能通过引导EntityManagerFactor来获得,EntityManagerFactor随后向应用程序类加载器注册,该应用程序类加载器将随后加载的任何类传递给转换器,然后再将其提供给运行时

问题是,此时在应用程序启动阶段,资源引用绑定尚未建立。因此,在其和定义中使用java:comp/env引用的持久化单元依赖于尚未解析的资源

为了解决这个问题,对于使用java:comp/env数据源的持久化单元,WebSphere引导一个EntityManagerFactory,专门用于收集类转换器(因此,它不会用于创建服务应用程序注入的EntityManager,如果使用具有全局JNDI名称的数据源,通常会出现这种情况),如果JPA持久性提供程序请求数据源(通常情况下,确定数据库供应商),则不会使用它,它提供了一个模拟数据源,即您在日志中看到的GenericDataSource。OpenJPA和Eclipselink不会产生错误,但Hibernate会产生错误

应用程序仍然可以工作的原因是,此EntityManagerFactory仅用于获取和注册类转换器,而不是用于为应用程序注入提供EntityManager,将创建一个新的EntityManagerFactory,用于在该组件的上下文中查找数据源


这就是为什么,尽管日志中有错误,您的应用程序仍然可以工作。

您看到的是,Hibernate在尝试使用GenericConnection时抛出异常,GenericConnection是java.sql.Connection的WebSphere内部模型。您看到,当WebSphere启动应用程序时,它必须尽早注册类转换器n在应用程序启动过程中,因为一旦应用程序类加载器加载了一个类,它就不再符合进一步转换的条件(JPA持久性提供程序可能会增强支持持久性的类,并添加内部操作所需的其他方法和字段)。类转换器只能通过引导EntityManagerFactor来获得,EntityManagerFactor随后向应用程序类加载器注册,该应用程序类加载器将随后加载的任何类传递给转换器,然后再将其提供给运行时

问题是,此时在应用程序启动阶段,资源引用绑定尚未建立。因此,在其和定义中使用java:comp/env引用的持久化单元依赖于尚未解析的资源

为了解决这个问题,对于使用java:comp/env数据源的持久化单元,WebSphere引导一个EntityManagerFactory,专门用于收集类转换器(因此,它不会用于创建服务应用程序注入的EntityManager,如果使用具有全局JNDI名称的数据源,通常会出现这种情况),如果JPA持久性提供程序请求数据源(通常情况下,确定数据库供应商),则不会使用它,它提供了一个模拟数据源,即您在日志中看到的GenericDataSource。OpenJPA和Eclipselink不会产生错误,但Hibernate会产生错误

应用程序仍然可以工作的原因是,此EntityManagerFactory仅用于获取和注册类转换器,而不是用于为应用程序注入提供EntityManager,将创建一个新的EntityManagerFactory,用于在该组件的上下文中查找数据源


这就是为什么,尽管日志中有错误,您的应用程序仍然可以工作。

谢谢@fyrewyld…非常感谢!有没有办法避免此异常?如果有,答案将在Hibernate的文档中找到。他们不会抛出一个冒泡回JPAPUnitInfo.createEMFactory()的异常(如果是这样的话,我希望这会影响应用程序的启动),所以我想象他们正在捕获自己的异常并将其记录到Sys Out/Err。对此我们无能为力。非常感谢您匹配@fyrewyld。我可以问您另一个问题吗?对于数据源,上述配置是否是在WAS 9中实现间接JNDI绑定的正确方法?是的,如果您不希望持久性单元依赖在全局JNDI名称上,那么使用java:comp/env/的资源引用是唯一的方法。谢谢@fyrewyld…太好了!有没有办法避免此异常?如果有,答案可以在Hibernate的文档中找到。他们不会抛出一个异常,该异常会冒泡回JPAPUnitInfo.createmFactory()(如果是这样的话,我希望这会影响应用程序的启动),所以我想象他们正在捕获自己的异常并将其记录到Sys Out/Err。对此我们无能为力。非常感谢matc
<resource-ref name="jdbc/RESOURCE_L" binding-name="jdbc/DATAS_ON_WAS" />
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:comp/env/jdbc/RESOURCE_L</jta-data-source>
 ERROR IdTableHelper:94 - Unable to use JDBC Connection to create Statement
java.sql.SQLException: Unsupported use of GenericConnection.  A GenericConnection is provided during application start when creating an EntityManagerFactory for a persistence unit which has configured one of its datasource to be in the component naming context; java:comp/env. During application start, the component naming context will not exist, and the correct datasource cannot be determined. When the persistence unit is used, the proper datasource and connection will be obtained and used.
    at com.ibm.ws.jpa.management.GenericConnection.unsupportedUseSQLException(GenericConnection.java:636)
    at com.ibm.ws.jpa.management.GenericConnection.createStatement(GenericConnection.java:144)
    at org.hibernate.hql.spi.id.IdTableHelper.executeIdTableCreationStatements(IdTableHelper.java:77)
    at org.hibernate.hql.spi.id.global.GlobalTemporaryTableBulkIdStrategy.finishPreparation(GlobalTemporaryTableBulkIdStrategy.java:125)
    at org.hibernate.hql.spi.id.global.GlobalTemporaryTableBulkIdStrategy.finishPreparation(GlobalTemporaryTableBulkIdStrategy.java:42)
    at org.hibernate.hql.spi.id.AbstractMultiTableBulkIdStrategyImpl.prepare(AbstractMultiTableBulkIdStrategyImpl.java:88)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:303)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:467)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:939)
    at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:149)
    at com.ibm.ws.jpa.management.JPAPUnitInfo.createEMFactory(JPAPUnitInfo.java:1161)
    at com.ibm.ws.jpa.management.JPAPUnitInfo.initialize(JPAPUnitInfo.java:984)
    at com.ibm.ws.jpa.management.JPAPxmlInfo.extractPersistenceUnits(JPAPxmlInfo.java:240)
    at com.ibm.ws.jpa.management.JPAScopeInfo.processPersistenceUnit(JPAScopeInfo.java:115)
    at com.ibm.ws.jpa.management.JPAApplInfo.addPersistenceUnits(JPAApplInfo.java:147)
    at com.ibm.ws.jpa.management.SharedJPAComponentImpl.startingModule(SharedJPAComponentImpl.java:322)
    at com.ibm.ws.jpa.management.JPAComponentImpl.startingDeployedModule(JPAComponentImpl.java:786)
    at com.ibm.ws.jpa.management.JPAComponentImpl.startingDeployedApplication(JPAComponentImpl.java:691)
    at com.ibm.ws.jpa.management.JPAComponentImpl.stateChanged(JPAComponentImpl.java:551)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl$ComparableDeployedObjectListener.stateChanged(ApplicationMgrImpl.java:2652)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.stateChanged(ApplicationMgrImpl.java:1178)
    at com.ibm.ws.runtime.component.DeployedApplicationImpl.fireDeployedObjectEvent(DeployedApplicationImpl.java:1561)
    at com.ibm.ws.runtime.component.DeployedApplicationImpl.setState(DeployedApplicationImpl.java:320)
    at com.ibm.ws.runtime.component.DeployedApplicationImpl.start(DeployedApplicationImpl.java:1144)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplication(ApplicationMgrImpl.java:800)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplicationDynamically(ApplicationMgrImpl.java:1450)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:2311)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.start(CompositionUnitMgrImpl.java:436)
    at com.ibm.ws.runtime.component.CompositionUnitImpl.start(CompositionUnitImpl.java:123)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.start(CompositionUnitMgrImpl.java:379)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.access$500(CompositionUnitMgrImpl.java:127)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl$1.run(CompositionUnitMgrImpl.java:654)
    at com.ibm.ws.security.auth.ContextManagerImpl.runAs(ContextManagerImpl.java:5574)
    at com.ibm.ws.security.auth.ContextManagerImpl.runAsSystem(ContextManagerImpl.java:5700)
    at com.ibm.ws.security.core.SecurityContext.runAsSystem(SecurityContext.java:255)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.startCompositionUnit(CompositionUnitMgrImpl.java:668)
    at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.startCompositionUnit(CompositionUnitMgrImpl.java:612)
    at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplication(ApplicationMgrImpl.java:1340)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:90)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
    at java.lang.reflect.Method.invoke(Method.java:508)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:83)
    at sun.reflect.GeneratedMethodAccessor36.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
    at java.lang.reflect.Method.invoke(Method.java:508)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:287)
    at javax.management.modelmbean.RequiredModelMBean$4.run(RequiredModelMBean.java:1263)
    at java.security.AccessController.doPrivileged(AccessController.java:673)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at javax.management.modelmbean.RequiredModelMBean.invokeMethod(RequiredModelMBean.java:1257)
    at javax.management.modelmbean.RequiredModelMBean.invoke(RequiredModelMBean.java:1096)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:831)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:813)
    at com.ibm.ws.management.AdminServiceImpl$1.run(AdminServiceImpl.java:1350)
    at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
    at com.ibm.ws.management.AdminServiceImpl.invoke(AdminServiceImpl.java:1243)
    at com.ibm.ws.management.application.sync.StartDeploymentTask.startDeployment(StartDeploymentTask.java:249)
    at com.ibm.ws.management.application.sync.StartDeploymentTask.fullAppUpdate(StartDeploymentTask.java:121)
    at com.ibm.ws.management.application.sync.StartDeploymentTask.performTask(StartDeploymentTask.java:109)
    at com.ibm.ws.management.application.sync.AppBinaryProcessor$ExpandApp.expand(AppBinaryProcessor.java:1770)
    at com.ibm.ws.management.application.sync.AppBinaryProcessor.postProcessSynchronousExt(AppBinaryProcessor.java:811)
    at com.ibm.ws.management.bla.sync.BLABinaryProcessor.postProcess(BLABinaryProcessor.java:599)
    at com.ibm.ws.management.bla.sync.BLABinaryProcessor.onChangeCompletion(BLABinaryProcessor.java:476)
    at com.ibm.ws.management.bla.sync.BinaryProcessorWrapper.onChangeCompletion(BinaryProcessorWrapper.java:109)
    at com.ibm.ws.management.repository.FileRepository.postNotify(FileRepository.java:1938)
    at com.ibm.ws.management.repository.FileRepository.update(FileRepository.java:1442)
    at com.ibm.ws.management.repository.client.LocalConfigRepositoryClient.update(LocalConfigRepositoryClient.java:189)
    at com.ibm.ws.sm.workspace.impl.WorkSpaceMasterRepositoryAdapter.update(WorkSpaceMasterRepositoryAdapter.java:667)
    at com.ibm.ws.sm.workspace.impl.RepositoryContextImpl.update(RepositoryContextImpl.java:1998)
    at com.ibm.ws.sm.workspace.impl.RepositoryContextImpl.synch(RepositoryContextImpl.java:1946)
    at com.ibm.ws.sm.workspace.impl.WorkSpaceImpl.synch(WorkSpaceImpl.java:549)
    at com.ibm.ws.console.core.action.SyncWorkSpaceAction$1.run(SyncWorkSpaceAction.java:284)
    at com.ibm.ws.security.auth.ContextManagerImpl.runAs(ContextManagerImpl.java:5574)
    at com.ibm.ws.security.auth.ContextManagerImpl.runAsSystem(ContextManagerImpl.java:5700)
    at com.ibm.ws.security.core.SecurityContext.runAsSystem(SecurityContext.java:255)
    at com.ibm.ws.console.core.action.SyncWorkSpaceAction.execute(SyncWorkSpaceAction.java:288)
    at org.apache.struts.action.RequestProcessor.processActionPerform(Unknown Source)
    at org.apache.struts.action.RequestProcessor.process(Unknown Source)
    at org.apache.struts.action.ActionServlet.process(Unknown Source)
    at org.apache.struts.action.ActionServlet.doGet(Unknown Source)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1235)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:779)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:143)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:96)
    at com.ibm.ws.console.core.servlet.WSCUrlFilter.setUpCommandAssistance(WSCUrlFilter.java:971)
    at com.ibm.ws.console.core.servlet.WSCUrlFilter.continueStoringTaskState(WSCUrlFilter.java:518)
    at com.ibm.ws.console.core.servlet.WSCUrlFilter.doFilter(WSCUrlFilter.java:339)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:197)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:90)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:969)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1109)
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:4217)
    at com.ibm.ws.webcontainer.webapp.WebAppImpl.handleRequest(WebAppImpl.java:2208)
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1030)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1817)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:382)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:465)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:532)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:318)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:289)
    at com.ibm.ws.ssl.channel.impl.SSLConnectionLink.determineNextChannel(SSLConnectionLink.java:1187)
    at com.ibm.ws.ssl.channel.impl.SSLConnectionLink$MyReadCompletedCallback.complete(SSLConnectionLink.java:694)
    at com.ibm.ws.ssl.channel.impl.SSLReadServiceContext$SSLReadCompletedCallback.complete(SSLReadServiceContext.java:1833)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1909)
INFO  LogHelper:31 - HHH000204: Processing PersistenceUnitInfo [
    name: mypersistence
    ...]