gpt4 book ai didi

spring - Liferay 服务构建器 JNDI 查找使用了错误的命名上下文

转载 作者:行者123 更新时间:2023-11-28 22:55:28 24 4
gpt4 key购买 nike

我有一个通过 JNDI 配置的 servicebuilder portlet:

<Context antiJARLocking="true" useHttpOnly="true">
<ResourceLink name="app/url" global="my-app/app/url"
type="java.lang.String" />
</Context>

在我的 ext-spring.xml 中,我尝试使用以下方法获取该值:

<jee:jndi-lookup jndi-name="java:comp/env/app/url" />

然而,用于查找此值的InitialContext似乎是ROOT webapp(liferay本身)的InitialContext因为它唯一包含的是 java:comp/env/jdbc/LiferayPool 数据库连接池。我知道 JNDI 上下文与由以下因素确定的 ClassLoader 相关联:

Thread.currentThread().getContextClassLoader();

而且我知道 liferay 在运行 portlet 初始化之前会破坏类加载器。但是对于我的一生,我无法弄清楚如何解决这个问题。有什么建议吗?

最佳答案

我为您提供了一个可行的解决方案,但它与您在帖子中描述的略有不同。

首先,我建议你将JNDI的声明移动到Tomcat的server.xml中,具体到 标签中。

这是一个例子:

<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource
name="jdbc/CustomDBPoolShared"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@YOUR_SERVER:1521:YOUR_SERVICE"
username="USERNAME"
password="PASSWORD"
maxActive="20"
maxIdle="5"
maxWait="10000"
/>
......
</GlobalNamingResources>

之后你应该在 context.xml 中创建一个ResourceLink:

<Context>

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>

<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->

<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<ResourceLink name="jdbc/CustomDBPool" global="jdbc/CustomDBPoolShared" type="javax.sql.DataSource"/>
....
</Context>

如果您有两个 portlet 访问同一个数据库,这将帮助您处理并发访问。

最后,ext-spring.xml:

   <!-- Custom Beans -->
<bean id="digitalHibernateSessionFactory" class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration" lazy-init="true">
<property name="dataSource">
<ref bean="customDBDataSource"/>
</property>
</bean>
<bean id="customDBSessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl" lazy-init="true">
<property name="sessionFactoryImplementor">
<ref bean="digitalHibernateSessionFactory" />
</property>
</bean>
<bean id="customDBDataSourceTarget" class="com.liferay.portal.spring.jndi.JndiObjectFactoryBean" lazy-init="true">
<property name="jndiName">
<value>jdbc/customDBPool</value>
</property>
</bean>
<bean id="customDBDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" lazy-init="true">
<property name="targetDataSource">
<ref bean="customDBDataSourceTarget" />
</property>
</bean>

注意 jdbc/自定义数据库池

请注意 jdbc/customDBPool,它在 context.xml 中有描述,它不仅适用于 ROOT 类加载器。

最后,您应该像这样在 LiferayServiceBuilder 的 service.xml 中引用正确的数据源:

<entity name="MyCustomEntity" table="CUSTOM_ENTITY" local-service="true" remote-service="false" data-source="customDBDataSource" session-factory="customDBSessionFactory" cache-enabled="false">
.....
</entity>

关于spring - Liferay 服务构建器 JNDI 查找使用了错误的命名上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28283994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com