我正在尝试在 JBoss 7.1 上的 GateIn 3.2.0 中部署的 Portlet 中获取数据源。
首先,我在独立数据源中创建.xml
<datasource jta="true" jndi-name="java:jboss/datasources/ccr" pool-name="ccr-pool" enabled="true" use-java-context="false" use-ccm="true">
<connection-url>jdbc:mysql://localhost:42006/contentrepository</connection-url>
<driver>com.mysql</driver>
<security>
<user-name>node</user-name>
</security>
<statement>
<prepared-statement-cache-size>100</prepared-statement-cache-size>
<share-prepared-statements>true</share-prepared-statements>
</statement>
</datasource>
我还添加了驱动程序和驱动程序模块。
在我的 portlet 中,我尝试使用
dataSource = (DataSource) new InitialContext().lookup("java:jboss/datasources/ccr");
但我只得到一个javax.nameing.NameNotFoundException:在上下文"中找不到名称"jboss"。
我也试图用
dataSource = (DataSource) new InitialContext().lookup("java:comp/env/datasources/ccr");
并将其与 Web.xml 和 JBoss-Web 中的以下条目映射.xml
网站.xml:
<resource-ref>
<description>MySQL DS</description>
<res-ref-name>datasources/ccr</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
jboss-web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<resource-ref>
<res-ref-name>datasources/ccr</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<jndi-name>java:jboss/datasources/ccr</jndi-name>
</resource-ref>
</jboss-web>
然后我得到了javax.naming.NameNotFoundException:在上下文"中找不到名称"comp"。
我错过了什么?是否有某些安全设置阻止我的应用程序提取数据源?
奇怪的是,当使用 servlet 时,我可以在同一个 Web 应用程序中获取数据源,但只能在 doGet/doPost 方法中获取数据源,而不是在 init() 中。
我在 GateIn 3.3 上遇到了同样的问题。但它没有用GateIn 3.4M1复制。当我谷歌这个错误时,我在JIRA中找到了它:https://issues.jboss.org/browse/GTNPORTAL-2511