[英]referring Tomcat JNDI datasource in persistence.xml
in server.xml I've defined global resource (I'm using Tomcat 6 ): 在server.xml中我已经定义了全局资源(我使用的是Tomcat 6 ):
<GlobalNamingResources>
<Resource name="jdbc/myds" auth="Container"
type="javax.sql.DataSource"
maxActive="10" maxIdle="3" maxWait="10000"
username="sa" password=""
driverClassName="org.h2.Driver"
url="jdbc:h2:~/.myds/data/db"
/>
</GlobalNamingResources>
I see in catalina.out that this is bound, so I suppose it's OK. 我在catalina.out中看到这是绑定的,所以我想它没关系。
In my web app I have the link to the datasource, I'm not sure it's OK: 在我的网络应用程序中,我有数据源的链接,我不确定它是否正常:
<Context>
<ResourceLink global='jdbc/myds' name='jdbc/myds' type="javax.sql.Datasource"/>
</Context>
and in application there is persistence.xml: 在应用程序中有persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="oam" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>jdbc/myds</non-jta-data-source>
<!-- class definitions here, nothing else -->
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
</properties>
</persistence-unit>
</persistence>
It should be OK, but most probably this or the ResourceLink definition is wrong because I'm getting: 它应该没问题,但很可能这个或者ResourceLink定义是错误的,因为我得到了:
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
javax.naming.NameNotFoundException:名称jdbc未绑定在此Context中
What's wrong and why this does not work? 有什么不对,为什么这不起作用?
UPDATE: 更新:
I've tried to get the datasource directly: 我试图直接获取数据源:
public class WebAppListener implements ServletContextListener
{
// ServletContextListener interface - start
public void contextInitialized(ServletContextEvent sce)
{
try
{
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
envCtx.lookup("jdbc/myds");
}
catch (NamingException ex)
{
System.out.println("!!!! Got NamingException:");
ex.printStackTrace(System.out);
}
}
public void contextDestroyed(ServletContextEvent sce) { }
}
my web.xml: 我的web.xml:
<listener>
<display-name>Listener</display-name>
<listener-class>WebAppListener</listener-class>
</listener>
Still getting the same error although I see the datasource in JMX console when I connect to the Tomcat (Catalina - Datasource - javax.sql.Datasource = "jdbc/myds" : ObjectName = Catalina:type=DataSource,class=javax.sql.DataSource,name="jdbc/myds". ) 当我连接到Tomcat时,我在JMX控制台中看到数据源时仍然得到相同的错误(Catalina - Datasource - javax.sql.Datasource =“jdbc / myds”:ObjectName = Catalina:type = DataSource,class = javax.sql。 DataSource,name =“jdbc / myds”。)
The <non-jta-data-source>
in persistence.xml
should be persistence.xml
的<non-jta-data-source>
应该是
java:comp/env/jdbc/myds
as per the response in http://forums.oracle.com/forums/thread.jspa?messageID=1899677 根据http://forums.oracle.com/forums/thread.jspa?messageID=1899677中的回复
And also is your db driver in $CATALINA_HOME/lib
并且也是
$CATALINA_HOME/lib
数据库驱动程序
(Im using Apache OpenJPA library in Tomcat7 so may not match to Hibernate stuff) (我在Tomcat7中使用Apache OpenJPA库,所以可能与Hibernate的东西不匹配)
I have never used a global jdbc for my OpenJPA webapps but gave it a try. 我从来没有在我的OpenJPA webapps上使用全局jdbc,但尝试了一下。 It worked and this is my configuration.
它工作,这是我的配置。 See folder where persistence.xml is saved, probably is openjpa problem but without it nothing works.
请参阅保存persistence.xml的文件夹,可能是openjpa问题,但没有它没有任何作用。
myapp/WEB-INF/classes/META-INF/persistence.xml MyApp的/ WEB-INF /类/ META-INF / persistence.xml中
this is using openjpa provider so class list may not be needed in hibernate. 这是使用openjpa提供程序,因此在hibernate中可能不需要类列表。
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<persistence-unit name="main" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<non-jta-data-source>java:comp/env/jdbc/mydb</non-jta-data-source>
<class>com.myapp.db.User</class>
<class>com.myapp.db.Server</class>
<properties>
<property name="openjpa.DynamicEnhancementAgent" value="false" />
<property name="openjpa.RuntimeUnenhancedClasses" value="unsupported" />
<property name="openjpa.Log" value="commons" />
<property name="openjpa.ConnectionFactoryProperties" value="PrintParameters=true" />
</properties>
</persistence-unit>
</persistence>
tomcat/conf/server.xml 的Tomcat / CONF / server.xml中
Add global jdbc resource. 添加全局jdbc资源。
..cut...
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" readonly="true" />
<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="20" maxWait="10000"
username="myuser" password="mypwd"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8"
validationQuery="SELECT 1" removeAbandoned="true" removeAbandonedTimeout="300"
/>
</GlobalNamingResources>
..cut...
tomcat/conf/Catalina/localhost/myapp.xml tomcat的/ conf目录/卡塔利娜/本地主机/ myapp.xml
this my development box so I use docBase to link directly to projects folder. 这是我的开发框,所以我使用docBase直接链接到项目文件夹。 You should be find inserting this to war package (META-INF/context.xml) when deployed to production box.
在部署到生产框时,您应该发现将此插入war包(META-INF / context.xml)。
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="/projects/myapp/web" reloadable="true" crossContext="true" >
<Realm className="org.apache.catalina.realm.DataSourceRealm"
dataSourceName="jdbc/mydb" localDataSource="false" digest="SHA"
userTable="user" userNameCol="username" userCredCol="password"
userRoleTable="user_role_v" roleNameCol="role"
/>
<ResourceLink name="jdbc/mydb" global="jdbc/mydb" type="javax.sql.DataSource" />
</Context>
myapp/WEB-INF/web.xml MyApp的/ WEB-INF / web.xml中
Add resource-ref to the end of file. 将resource-ref添加到文件末尾。
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" >
<description>Webapp</description>
<display-name>Webapp</display-name>
..cut...
<resource-ref>
<description>mydb</description>
<res-ref-name>jdbc/mydb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
Because Im using OpenJPA+Tomcat7 (not fullsized j2ee container) this may look overengineering but thats how it works. 因为我使用OpenJPA + Tomcat7(不是全尺寸的j2ee容器),这可能看起来过度工程,但这就是它的工作原理。 Results are good and developing db-aware webapps is very easy.
结果很好,开发db-aware webapps非常容易。 No need to manual sql query hardcoding and oldskool DAO classes.
无需手动sql查询硬编码和oldskool DAO类。
Did you make that resource available for the application by declaring it in your web.xml
? 您是否通过在
web.xml
声明该资源,为该应用程序提供了该资源?
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/myds</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.