I am trying to connect to my database using a Java DataSource. I am using Weblogic 12c and Hibernate to achieve this. My web.xml file contains the following resource definition:
<resource-ref>
<res-ref-name>jdbc/Microtek</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
In addition, my weblogic.xml contains the following:
<wls:resource-description>
<wls:res-ref-name>jdbc/Microtek</wls:res-ref-name>
<wls:jndi-name>microtek</wls:jndi-name>
</wls:resource-description>
In my Hibernate config file (hibernate.cfg.xml), I have set the following properties:
<property name="hibernate.connection.datasource">jdbc/Microtek</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="javax.persistence.validation.mode">none</property>
Also, inside the META-INF directory, I created an XML file called context.xml which contains the following:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<ResourceLink global="jdbc/Microtek" name="jdbc/Microtek" type="javax.sql.DataSource"/>
</Context>
Finally, this is the exception I have been getting:
org.hibernate.HibernateException: Could not find datasource
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:79)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:143)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:84)
at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:459)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:91)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2831)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2827)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1838)
at com.microtekcomputers.models.test.main(test.java:15)
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)
... 8 more
It is my first time using JNDI to connect to a data source so I might be missing something. All ideas & recommendations greatly appreciated.
UPDATE: After making the changes suggested by others, I am now getting the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: weblogic/kernel/KernelStatus
at weblogic.jndi.Environment.<clinit>(Environment.java:81)
at weblogic.jndi.WLInitialContextFactory.getInitialContext(WLInitialContextFactory.java:117)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.init(InitialContext.java:244)
at javax.naming.InitialContext.<init>(InitialContext.java:216)
at org.hibernate.util.NamingHelper.getInitialContext(NamingHelper.java:51)
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:143)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:84)
at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:459)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:91)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2831)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2827)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1838)
at com.microtekcomputers.models.Test.main(Test.java:12)
You do not have a connection factory defined for your data source, something like this in the code:
environment.put("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");
Or this in the hibernate.cfg.xml:
<property name="jndi.class">weblogic.jndi.WLInitialContextFactory</property>
In weblogic you can configure connection factories to your need, WLInitialContextFactory is the default.
Also see the Oracle docs and client examples
Related
My question is: How do I map a datasource to a specific jndi-name configured inside wildfly so that multiple deployed .war-files each can use their own specific datasource. The mapping should take place at deployment so that a configuration inside wildfly and the specific project is sufficient.
We got a project which supports multi-tenancy. The structure is as follows:
customerSpecificProject
|-- ui (generic)
|----database (generic)
|----services (generic)
|----etc...
In the database-project a standard datasource is specified which goes by a standard jndi-name java:/xyzDS. Since we migrated from tomcat to wildfly we want to make use of the ability to host multiple applications in our AS.
To achieve this we have to map the java:/xyzDS to the datasources defined in the standalone.xml:
<subsystem xmlns="urn:jboss:domain:datasources:4.0">
<datasources>
<datasource jta="true" jndi-name="java:/customer1DS" pool-name="c1DS" enabled="true" use-ccm="true">
...
</datasource>
<datasource jta="true" jndi-name="java:/customer2DS" pool-name="c2DS" enabled="true" use-ccm="true">
...
</datasource>
Therefore we tried to use the jboss-web.xml located in the WEB-INF folder of our customerProject:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.jboss.com/xml/ns/javaee
http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd">
<resource-ref>
<res-ref-name>java:/xyzDS</res-ref-name>
<jndi-name>java:/customer1DS</jndi-name>
</resource-ref>
</jboss-web>
persistence.xml inside the database-Project:
<?xml version='1.0' encoding='UTF-8'?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/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_1.xsd">
<persistence-unit name="xyzDB" transaction-type="JTA">
<jta-data-source>java:/xyzDS</jta-data-source>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>...</class>
...
<class>...</class>
<shared-cache-mode>NONE</shared-cache-mode>
<validation-mode>NONE</validation-mode>
<properties>
<property name="eclipselink.weaving" value="static" />
</properties>
</persistence-unit>
</persistence>
It does not seem to be working. I talked to a colleague who had a similar issue and resolved it in the corresponding way for a websphere AS. I do not know if the problem resides in the structure having a nested databaseProject inside another lib (ui) for the actual customerProject or if it is caused by bad configuration.
Also we use the #Resource Annotation inside some DAOs:
#Resource(lookup = "java:/xyzDS")
private DataSource dataSource;
Maybe the mapping works but not for the annotations inside the compiled DAOs? But I do not understand why that should not work.
Current Error in Stacktrace:
11:18:20,839 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 1) WFLYCTL0013: Operation ("full-replace-deployment") failed - address: ([]) - failure description: {
"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"xyz.war\".INSTALL" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"xyz.war\".INSTALL: WFLYSRV0153: Failed to process phase INSTALL of deployment \"xyz.war\"
Caused by: java.lang.IllegalArgumentException: WFLYEE0047: Incompatible conflicting binding at java:/xyzDS source: lookup (java:/customer1DS)"},
Edit: Already tried adding:
<property name="wildfly.jpa.twophasebootstrap" value="false"/>
To persistence.xml
Have you checked the question/answer in wildfly-development.1055759.n5.nabble.com? In this exchange, it was determined that the persistence unit needed a hint in the form of
<property name="wildfly.jpa.twophasebootstrap" value="false"/>
I don't know if this is applicable in your case, but is worth looking at.
I would like to configure a series of environment-dependant settings as an external resource, so that the same WAR artifact can be configured in any application server.
In Glassfish I rememeber that I just created a .properties file using the built-in Properties Resource JNDI Factory, and it worked like a charm, but there's no such thing in Tomcat.
I tried defining a java.net.URL and a java.lang.String to define just the path that this file is held in, but I get the following error:
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/c://app.properties]
So what would be a good way to create an environment-agnostic set of properties for my application?
PS: this is how I define my properties in Spring:
<jee:jndi-lookup id="appProperties" jndi-name="java:comp/env/url/urlAppProperties" />
<bean id="application-properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>#{ appProperties }</value>
</list>
</property>
</bean>
You can add external configuration values to the JNDI context using Tomcat's resource configuration elements. As described in the Tomcat documentation, you can define a <GlobalNamingResources> element in your server.xml listing a set of configuration values which are available to all contexts (web applications) on that server:
Example taken from the Tomcat docs:
<GlobalNamingResources ...>
...
<Environment name="maxExemptions" value="10"
type="java.lang.Integer" override="false"/>
...
</GlobalNamingResources>
These environment variables are then available via JDNI in the java:comp/env context. In order to use such variables in a web application, you have to link to them in the individual context.xml configuration files using the <ResourceLink> element.
<Context>
<ResourceLink name="maxExemptions" global="maxExemptions" type="java.lang.Integer"/>
</Context>
With this set up, you can access these values through the standard JNDI API.
Hello I'm using the following
hibernate-core-4.1.2.Final.jar
mysql-connector-5.1.6.jar
Both can be found in my project lib directory.
I have the following hibernate.cg.xml configuration.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Defines the SQL dialect used in Hiberante's application -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--Local Database Connection-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/domain</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">test</property>
<property name="hbm2ddl.auto">validate</property>
<property name="show_sql">false</property>
<property name="format_sql">false</property>
<property name="use_sql_comments">false</property>
<property name="hibernate.search.default.directory_provider">ram</property>
</session-factory>
</hibernate-configuration>
and I'm getting the following exception.
Caused by: java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost:3306/domain at
java.sql.DriverManager.getConnection(DriverManager.java:604) at
java.sql.DriverManager.getConnection(DriverManager.java:190) at
org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192)
at
org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:278)
at
org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
... 145 more
I do not want to use JNDI do to the fact management wants to keep the app as portable as possible, so what am I missing to get this to work with jdbc? Am I required to do any kind of configurations to tomcat?
Tomcat Lib
Try putting mysql-connector-5.1.6.jar directly into the lib folder of tomcat and restarting it.
Have you tried calling the driver class?:
Class.forName("com.mysql.jdbc.Driver");
How to call it:
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
// error out
}
Connection con = DriverManager.getConnection(/*your connection query*/);
I may have the class wrong, but if com.mysql.jdbc.Driver doesn't work, you can also try com.mysql.JDBC or com.mysql.jdbc (basing off how SQLite calls it)
Did you edit the config to obscure the connection string?
Your hibernate config has a different database name than the error:
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/domain</property>
Caused by: java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost:3306/etss
In terms of portability, I package the database driver with my war, so it is self contained. This makes for deployment across multiple environments much easier and if another developer wants to build and run locally, they just have to drop the war into Tomcat and go. Place the database driver in your WEB-INF/lib folder.
Also, in terms portability, I recommend JNDI... that way you do not have to edit your hibernate config file when you deploy it to another server and it can stay packaged in your war. You just add the JNDI reference in the Tomcat config.
The exception occurs because the mysql database driver is not on your classpath. Add it to your classpath to repair the issue. Since you are using tomcat you can simply add it to the tomcat/lib directory.
I would suggest putting your drivers in/at where you have place JDK's Extension directory. Please see:http://www3.ntu.edu.sg/home/ehchua/programming/howto/ErrorMessages.html#zz-4.1
Once that is done I would encourage you to from your prompt type:
echo %CLASSPATH%
I am trying to generate tables from my entities in Eclipse Juno using Project right-click -> JPA Tools -> Generate Tables from Entities
I am using EclipseLink as JPA Provider
My app container is Glassfish 4
My database is the one embedded within glassfish, derby.
My application starts without errors and I am able to commit data to the database using a simple test bean (so I assume my connection pool and datasource are working as intended)
I am getting the following stack trace error when I try to generate the tables from entities:
[EL Info]: EclipseLink, version: Eclipsenter code heree Persistence Services - 2.4.0.v20120608-r11652
[EL Severe]: ejb: Local Exception Stack:
Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/__planificateur].
Internal Exception: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at org.eclipse.persistence.exceptions.ValidationException.cannotAcquireDataSource(ValidationException.java:502)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:109)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:685)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:213)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:542)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:186)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:278)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:304)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:282)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.perform(Main.java:85)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.execute(Main.java:76)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.main(Main.java:63)
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:103)
... 11 more
Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/__planificateur].
Internal Exception: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:602)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:186)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:278)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:304)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:282)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.perform(Main.java:85)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.execute(Main.java:76)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.main(Main.java:63)
Caused by: Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/__planificateur].
Internal Exception: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at org.eclipse.persistence.exceptions.ValidationException.cannotAcquireDataSource(ValidationException.java:502)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:109)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:685)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:213)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:542)
... 7 more
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:103)
... 11 more
Here is my persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="planificateurepicerie" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/__planificateur</jta-data-source>
<class>com.whybe.pe.jpa.Ingredient</class>
<class>com.whybe.pe.jpa.Recette</class>
<class>com.whybe.pe.jpa.ComposantRecette</class>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
I also have the database setup in the Database Development perspective and that works fine too.
Inside the glassfish console I can ping the connection pool successfully.
I am running out of ideas as of why the ddl won't generate...
If you need more details let me know!
Thanks in advance!
You are using transaction-type="JTA" in a run mode as standalone application. JPA looking for the JNDI resource name in the default context (Wait, currently there is no context in your application, since is a run mode standalone application). Change to transaction-type="LOCAL" for that run mode. e.g.:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="SamplePU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/sample"/>
<property name="javax.persistence.jdbc.password" value="123"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
But you are using Glassfish 4. Run the application on the server. If you don't have the JDNI resource name, you need to add it in the JDBC resources. The more easy way is using the console administration of Glassfish. By default, you can find it in http://localhost:4848. See more about this in Chapter 3 JDBC Resources.
I'm using Hibernate 4.0 with a JPA persistence.xml file on Tomcat 7. No Struts, just straight Hibernate with some Jersey services. Here is the exception I'm running into:
Caused by: org.hibernate.service.jndi.JndiException: Unable to lookup JNDI name [jdbc/MyDB]
at org.hibernate.service.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:68)
at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:116)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:223)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2273)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2269)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1738)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
... 8 more
Caused by: javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:135)
at javax.naming.InitialContext.lookup(InitialContext.java:396)
at org.hibernate.service.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:65)
... 23 more
I see the note about jbc is not bound in this context, but I'm confused as how this is happening. I'm deploying my context in an app-specific context.xml, below:
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/MyDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="..." password="..." driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mydb"/>
</Context>
And my persistence.xml file looks like:
<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="com.example.mysql" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>jdbc/MyDB</non-jta-data-source>
<class>...</class>
<properties>
<property name="hibernate.connection.datasource" value="jdbc/MyDB"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.id.new_generator_mappings" value ="true"/>
</properties>
</persistence-unit>
</persistence>
Finally, my web.xml file has the resource defined as so:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>My Web Application</display-name>
<resource-ref>
<description>DB Connection</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>
As for my layout, here is how my war file is structured:
app.war
+ META-INF
- context.xml
+ WEB-INF
+ classes
+ META-INF
- persistence.xml
+ lib
- web.xml
A few minor notes:
Using a global context versus an application-specific context makes no difference.
The code trying to instantiate an EntityManager instance is in a JAR file in the lib directory (part of a multi-project Maven build), but the persistence XML is in the main web app as outlined above.
I can see the JNDI datasource in Tomcat and I can query it using psi-probe, i.e. I can access the connection information and successfully execute SQL queries against the data source.
Since you use a portable resource with , you should call your JNDI using "java:comp/env/your_resource", like java:comp/env/jdbc/MyDB