我为您提供了一个可行的解决方案,但它与您在帖子中描述的有所不同。
首先,我建议你把JNDI的声明移到Tomcat的server.xml中,具体到<GlobalNamingResources>标签中。
这是一个例子:
<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/customDBPool
请注意在context.xml中描述的jdbc/customDBPool ,它不仅适用于 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>