我需要一些帮助让Hibernate 4与Weblogic一起工作。我们有一个使用Hibernate 3.6.10在weblogic上运行良好的应用程序。当我们升级到Hibernate 4.3.11时,它将不再找到Weblogic JNDI数据源。相同的应用程序适用于Tomcat。我们正在使用Weblogic版本10.3.6.0(11g)进行测试。该包是一个WAR文件。
我们在查找中使用了一些间接,以便我们可以使用部署计划来提供从内部逻辑数据源名称到实际Weblogic datsource的链接。
创建会话的代码如下所示(java):
private SessionFactory startHibernateTransaction()
{
SessionFactory hibernateSessionFactory = null;
ServiceRegistry serviceRegistry;
Configuration configuration = new Configuration();
configuration.configure(“StandAloneHibernate.cfg.xml”);
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
hibernateSessionFactory = configuration.buildSessionFactory(serviceRegistry);
// starts hibernate transaction (needed to use iterator())
hibernateSessionFactory.getCurrentSession().beginTransaction();
return hibernateSessionFactory;
}
Hibernate配置文件是:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.datasource">java:comp/env/dwDatasource</property>
<property name="hibernate.dialect">CustomDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">false</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<mapping resource="hibernate/ShpSettingsMst.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
我们的web.xml文件(删节):
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: //Tuxedo/RELEASE/Product/Fast_Forward/DashboardServlet/src/main/webapp/WEB-INF/web.xml#3 $ -->
<!-- Copyright 2015 Ellucian Company L.P. and its affiliates. -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
...
<resource-ref>
<description>Data source</description>
<res-ref-name>dwDatasource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
我们的weblogic.xml:
<?xml version="1.0"?>
<!-- $Id: //Tuxedo/RELEASE/Product/Fast_Forward/DashboardServlet/src/main/webapp/WEB-INF/weblogic.xml#1 $ -->
<!-- Copyright 2015 Ellucian Company L.P. and its affiliates. -->
<weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/90" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<container-descriptor>
<prefer-web-inf-classes>true</prefer-web-inf-classes>
<!-- Enables weblogic to use ServletContext.realPath(...) to locate local configuration files -->
<show-archived-real-path-enabled>true</show-archived-real-path-enabled>
</container-descriptor>
<resource-description>
<res-ref-name>dwDatasource</res-ref-name>
<jndi-name>jdbc/EGdwDatasource</jndi-name>
</resource-description>
</weblogic-web-app>
使用JNDI名称“jdbc / EGdwDatasource”定义Weblogic数据源。
如果我只将“jdbc / EGdwDatasource”放入StandAloneHibernate.cfg.xml文件(没有“java:comp / env /”),那么它会找到数据源。但是,我们需要间接,因为我们希望使用部署计划来更改数据源而无需更改WAR文件。
我为res-ref-name尝试了许多不同的组合,包括“jdbc / dwDatasource”。我试过不用“comp / env”(例如“java:dwDatsource”)。
我在weblogic中启用了JNDI调试,并看到一些有趣的输出,但是,如果没有代码,我真的不知道它告诉我什么。我看到例如:
<Mar 29, 2017 10:32:21 PM EDT> <Debug> <JNDI> <BEA-000000> <+++ bind(app/webapp/dashboard.war/1049307908/comp/env/dwDatasource, javax.naming.LinkRef) succeeded>
如果失败,我会看到消息:
Caused by: javax.naming.NameNotFoundException: While trying to look up /comp.env.dwDatasource in /app/webapp/dashboard.war/1049307908.; remaining name '/comp/env/dwDatasource'
鉴于这两个陈述,它看起来应该找到该条目。 这是错误和堆栈跟踪:
29-Mar-2017 10:32:26:316 EDT [[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'] ERROR ContextLoader.initWebApplicationContext(351): Context initialization failed
org.hibernate.engine.jndi.JndiException: Unable to lookup JNDI name [java:comp/env/dwDatasource]
at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:117)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:115)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
at net.hedtech.degreeworks.settings.dao.ShpSettingsDaoStandalone.startHibernateTransaction(ShpSettingsDaoStandalone.java:125)
at net.hedtech.degreeworks.settings.dao.ShpSettingsDaoStandalone.get(ShpSettingsDaoStandalone.java:44)
at net.hedtech.degreeworks.settings.util.TracingPlaceholderConfigurer.setupCrypto(TracingPlaceholderConfigurer.java:787)
at net.hedtech.degreeworks.settings.util.TracingPlaceholderConfigurer.loadPropertiesFromFactories(TracingPlaceholderConfigurer.java:257)
at net.hedtech.degreeworks.settings.util.TracingPlaceholderConfigurer.postProcessBeanFactory(TracingPlaceholderConfigurer.java:181)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:166)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at weblogic.servlet.internal.EventsManager$FireContextListenerAction.run(EventsManager.java:481)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.internal.EventsManager.notifyContextCreatedEvent(EventsManager.java:181)
at weblogic.servlet.internal.WebAppServletContext.preloadResources(WebAppServletContext.java:1868)
at weblogic.servlet.internal.WebAppServletContext.start(WebAppServletContext.java:3154)
at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1518)
at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:484)
at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:425)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:52)
at weblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:119)
at weblogic.application.internal.flow.ScopedModuleDriver.start(ScopedModuleDriver.java:200)
at weblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:247)
at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:425)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:52)
at weblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:119)
at weblogic.application.internal.flow.StartModulesFlow.activate(StartModulesFlow.java:27)
at weblogic.application.internal.BaseDeployment$2.next(BaseDeployment.java:671)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:52)
at weblogic.application.internal.BaseDeployment.activate(BaseDeployment.java:212)
at weblogic.application.internal.SingleModuleDeployment.activate(SingleModuleDeployment.java:44)
at weblogic.application.internal.DeploymentStateChecker.activate(DeploymentStateChecker.java:161)
at weblogic.deploy.internal.targetserver.AppContainerInvoker.activate(AppContainerInvoker.java:79)
at weblogic.deploy.internal.targetserver.BasicDeployment.activate(BasicDeployment.java:184)
at weblogic.deploy.internal.targetserver.BasicDeployment.activateFromServerLifecycle(BasicDeployment.java:361)
at weblogic.management.deploy.internal.DeploymentAdapter$1.doActivate(DeploymentAdapter.java:51)
at weblogic.management.deploy.internal.DeploymentAdapter.activate(DeploymentAdapter.java:200)
at weblogic.management.deploy.internal.AppTransition$2.transitionApp(AppTransition.java:30)
at weblogic.management.deploy.internal.ConfiguredDeployments.transitionApps(ConfiguredDeployments.java:240)
at weblogic.management.deploy.internal.ConfiguredDeployments.activate(ConfiguredDeployments.java:169)
at weblogic.management.deploy.internal.ConfiguredDeployments.deploy(ConfiguredDeployments.java:123)
at weblogic.management.deploy.internal.DeploymentServerService.resume(DeploymentServerService.java:180)
at weblogic.management.deploy.internal.DeploymentServerService.start(DeploymentServerService.java:96)
at weblogic.t3.srvr.SubsystemRequest.run(SubsystemRequest.java:64)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
Caused by: javax.naming.NameNotFoundException: While trying to look up /comp.env.dwDatasource in /app/webapp/dashboard.war/1049307908.; remaining name '/comp/env/dwDatasource'
at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1139)
at weblogic.jndi.internal.ApplicationNamingNode.lookup(ApplicationNamingNode.java:144)
at weblogic.jndi.internal.WLEventContextImpl.lookup(WLEventContextImpl.java:254)
at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:412)
at weblogic.jndi.factories.java.ReadOnlyContextWrapper.lookup(ReadOnlyContextWrapper.java:45)
at weblogic.jndi.internal.AbstractURLContext.lookup(AbstractURLContext.java:135)
at javax.naming.InitialContext.lookup(InitialContext.java:415)
at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:114)
... 59 more
Hibernate中存在一个可能适用的问题https://hibernate.atlassian.net/browse/HHH-7012。它的主要焦点是Tomcat,解决方案是使用修复其JNDI查找的Tomcat版本。它提到Hibernate在4中做的更改是使用带有JNDI Name对象的lookup方法,而不是使用String的对象。
非常感谢帮助解决方案或如何进一步调试。
更新:如果我在自己的JndiService中替换,我能够使用它。我克隆了Hibernate,并将其查找更改为使用String而不是Name。他们是:
JndiServiceImpl
public Object locate(String jndiName) {
final InitialContext initialContext = buildInitialContext();
final Name name = parseName( jndiName, initialContext );
try {
return initialContext.lookup( name );
}
catch ( NamingException e ) {
我的是
public Object locate(String jndiName)
{
final InitialContext initialContext = buildInitialContext();
try
{
return initialContext.lookup(jndiName);
}
catch (NamingException e)
但是,我宁愿没有自定义的JndiService实现,所以如果有人知道任何其他解决方案,请告诉我。