in server.xml I've defined global resource (I'm using 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.
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 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:
javax.naming.NameNotFoundException: Name jdbc is not bound in this 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:
<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". )
The <non-jta-data-source>
in persistence.xml
should be
java:comp/env/jdbc/myds
as per the response in http://forums.oracle.com/forums/thread.jspa?messageID=1899677
And also is your db driver in $CATALINA_HOME/lib
(Im using Apache OpenJPA library in Tomcat7 so may not match to Hibernate stuff)
I have never used a global jdbc for my OpenJPA webapps but gave it a try. It worked and this is my configuration. See folder where persistence.xml is saved, probably is openjpa problem but without it nothing works.
myapp/WEB-INF/classes/META-INF/persistence.xml
this is using openjpa provider so class list may not be needed in 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
Add global jdbc resource.
..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
this my development box so I use docBase to link directly to projects folder. You should be find inserting this to war package (META-INF/context.xml) when deployed to production box.
<?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
Add resource-ref to the end of file.
<?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. Results are good and developing db-aware webapps is very easy. No need to manual sql query hardcoding and oldskool DAO classes.
Did you make that resource available for the application by declaring it in your 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>
来源:https://stackoverflow.com/questions/2911439/referring-tomcat-jndi-datasource-in-persistence-xml