Class [oracle.jdbc.OracleDriver] not found - java

I try to test Vaadin JPAContainer demo 'jpacontainer-addressbook-demo' to connect to oracle. But I met exception.
com.vaadin.server.ServiceException: javax.persistence.PersistenceException: Exception [EclipseLink-4003] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Configuration error. Class [oracle.jdbc.OracleDriver] not found.
My persistence.xml
<persistence-unit name="oracle">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:#localhost:1521:orcl"/>
<property name="javax.persistence.jdbc.user" value="xxx"/>
<property name="javax.persistence.jdbc.password" value="xxx"/>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
</properties>
</persistence-unit>
I have put ojdbc6.jar to WEB-INF/lib. And I cound found ojdbc6.jar in WEB-INF\lib\ of jpacontainer-addressbook-demo-3.2.0.war. I dont understand why it have this exception. Any hints would be much appreciated.
I found the solution by following this page.
http://www.mkyong.com/maven/how-to-add-oracle-jdbc-driver-in-your-maven-local-repository/
But I dont know why cant just directly put jar file to lib folder. Could any one help to explain? Thanks.

The configuration hints towards the driver not being loaded because of a configuration error.
Depending on your appserver you might have to change the database url.
We use Wildfly and Weblogic and there the URL uses a slash between server and database names:
jdbc:oracle:thin:#localhost:1521/orcl
And according to a post on the OTN it would be again different for Glassfish:
jdbc:oracle:thin://localhost:1521:orcl

I hope this help you
If you are using Eclipse then you can just include this JAR in your build path by selecting project, right click, properties, Java Build path and then adding JAR on libraries tab.

Related

why getting “No Persistence provider for EntityManager” when running with jnlp while standalone jar works fine

Environment
Win 8.1
MySQL server: localhost
Netbeans 7.4
Java: 1.7.0_51 (J2SE)
JRE: 1.7.0_51-b31
EclipseLink(JPA 2.1)
There are several posts asking about this “no persistence provider for EntityManager named…” error. I am developing with J2SE (not J2EE). However, my problems seem silly but it really troubles me for 2days. Is it that I need some other software for this standalone program to work? But why is it oaky when I run the program under Netbeans IDE? Is it some sort of environment setting issue which I failed to try?
Working snapshot and EclipseLink message!
I use Netbeans to write code, and it runs okay. Now, I decided to copy the whole standalone package out “D:\NetBeansWork\ProjCostTracking\dist”. I have change the security setting to medium in Java Control Panel. Then, I double-clicked on ProjCostTracking.jnlp to launch. Well, this is what I see.
this is my persistence.xml
<?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="ProjCostTrackingPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>ProjCostTracking.EntityUserlevel</class>
<class>ProjCostTracking.EntityUser</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/biotechcost?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.password" value="1234"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
</properties>
</persistence-unit>
</persistence>
The entity files are
The generated files and lib
As for persistence.xml, I copy to 3 places just to make sure it can be “seen”.
As for CLASSPATH, I copied the required jar files (from ProjCostTracking\dist\lib) to lib folder under my default JDK and JRE. So java suppose to see the jars.
Usually this is a connector issue.
It seems that eclipse is not using the right connector. Check the connector version. It should match with the version of your MySQL database.
I guess I am using JNLP in a wrong way. Because when I run the standalone with "java -jar myprogram.jar" it works perfectly, no error. But, when I use jnlp it gives me the error. Hope I am not misleading everyone here. It is more related to how jnlp works with the persistence stuff.
The Problem is that if you want to use Application Managed Entity Manager you have to control the scope of the drivers and used APIs.
For example: in a Java SE Application you can build the project with maven. The Scope of the dependencies signals when the API or the package has to be used. E.g.
(...)
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.1.Final</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.1.Final</version>
<scope>runtime</scope>
</dependency>
(...)
Because the life circle is managed by the application and not by the container (like in Java EE applications).
In my case it was important to change the scope from provided to runtime (Same with the database driver). In your case - if you are not using maven you are dependent what the JVM is using.
Maybe this is helpful. I know this kind of issues and it is really annoying :|
Maven Scope Doc

Configuring data source - tomee.xml, persistence.xml

I'm doing some EJB with JPA project that maps/persists some entities to mysql database.
I have defined persistence unit in persistence.xml like this:
<persistence-unit name="MyAppPU" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>MyAppDS</jta-data-source>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
<property name="openjpa.jdbc.DBDictionary" value="mysql" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
</properties>
</persistence-unit>
Then, in tomee/conf/tomee.xml file i have defined data source like this:
<Resource id="MyAppDS" type="DataSource">
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql://127.0.0.1:3306/MyAppDB
UserName root
Password 123
JtaManaged true
DefaultAutoCommit false
</Resource>
All this works fine, i create MyApp.jar, deploy it to TomEE server, test it and i get mysql tables in database.
My question is "Is there any other place where I could define data source resource?"
Or it has to be in tomee/conf/tomee.xml file?
Can it be defined somewhere inside application structure, in some xml file, and deployed inside apps jar file to server?
That's the whole point of a JNDI data source, to externalize it outside of your application, so you can modify it without recompiling or repackaging. So it is better to leave it this way.
For testing purpose, some EE server such as JBoss (Wildfly) let you define this in your project.
It might be a bit late to answer this, You can in tomee place the resource definition in WEB-INF/resources.xml.
You only have to set the tomee.xml. If you are using Eclipse, you must copy the tomee.xml into the servers configuration/Tomee to get recognize it into the web project, otherwise you will get troubles.

No Persistence provider for EntityManager named X

I am developing a JavaSE application using JPA. Unfortunately, I get null after calling:
Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
Below you will find:
A snippet of my code that invokes EntityManagerFactory and unexpectedly returns null
My persistence.xml file
My project structure
Snippet of my code:
public class Main {
private static final String PERSISTENCE_UNIT_NAME = "MeineJpaPU";
private static EntityManagerFactory factory;
public static void main(String[] args) {
// I get null on this line!!!
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
// do stuff with entity manager
...
}
}
My persistence.xml:
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="MeineJpaPU" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>path.to.package.server.Todo</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/test"/>
<property name="javax.persistence.jdbc.user" value="postgres"/>
<property name="javax.persistence.jdbc.password" value="postgres"/>
</properties>
</persistence-unit>
</persistence>
My project structure:
You must move persistence.xml file to an appropriate location.
More specifically, add META-INF/persistence.xml file to the root of a source folder.
In this case, the following is an appropriate location: src\main\java\META-INF\persistence.xml
Here are the details:
(taken from the JPA spec)
A persistence.xml file defines a persistence unit. The persistence.xml
file is located in the META-INF directory of the root of the
persistence unit.
The root of the persistence unit is the key here.
If you are a non-Java EE app
The jar file or directory whose META-INF directory contains the
persistence.xml file is termed the root of the persistence unit.
If you are in a Java EE app, the following are valid
In Java EE environments, the root of a persistence unit must be one of
the following:
an EJB-JAR file
the WEB-INF/classes directory of a WAR file[80]
a jar file in the WEB-INF/lib directory of a WAR file
a jar file in the EAR library directory
an application client jar file
Quick advice:
check if persistence.xml is in your classpath
check if hibernate provider is in your classpath
With using JPA in standalone application (outside of JavaEE), a persistence provider needs to be specified somewhere. This can be done in two ways that I know of:
either add provider element into the persistence unit: <provider>org.hibernate.ejb.HibernatePersistence</provider> (as described in correct answer by Chris here: https://stackoverflow.com/a/1285436/784594)
or provider for interface javax.persistence.spi.PersistenceProvider must be specified as a service, see here: http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html (this is usually included when you include hibernate,or another JPA implementation, into your classpath
In my case, I found out that due to maven misconfiguration, hibernate-entitymanager.jar was not included as a dependency, even if it was a transient dependency of other module.
See also answers here: No Persistence provider for EntityManager named
So in my case, everything was in the class path, but I had to add
Class c = Class.forName("org.eclipse.persistence.jpa.PersistenceProvider");
I think this caused the PersistenceProvider to register itself with the javax classes. I have had to do something similar for JDBC drivers in the past as well.
I recently upgraded NB 8.1 to 8.2 and suddenly faced this problem and spent 2 days breaking my head to resolve. Up to 8.1, removing processorpath (mentioned above by others) worked. With 8.2, the problem persisted.
Finally, I found that the eclipselink.jar is missing in the default library of EclipseLink (JPA 2.1). I added the file to the definition of the library and voila - it started working!
If you don't want to move your META-INF folder (maybe because that's where your IDE created it and you don't want to confuse it), and if you are using Maven, you can tell Maven where to look for META-INF using the <resources>tag. See: http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html
In your case:
<build>
<resources>
<resource>
<directory>src</directory>
</resource>
</resources>
</build>
Dont give JPA dependency explicity
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
Thanks,
Rahul

Configure hibernate.cfg.xml in OpenShift RedHat servers

I'm trying to configure a xWiki server on a OpenShift hosting (Tomcat 6 (JBoss EWS 1.0)). I've never configured a Java server before and I have a issue:
I followed THIS tuto on my local Ubuntu and worked. But reproducing the steps in the OpenShift server I can't find the hibernate.cfg.xml. Looking for files in the directory tree with hibernate I've found the folder jbossews-1.0/jbossews-1.0/work/Catalina/localhost/xwiki/WEB-INF/lib/ with the files:
hibernate-c3p0-3.6.9.Final.jar
hibernate-core-3.6.9.Final.jar
hibernate-validator-4.3.0.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-commons-annotations-3.2.0.Final.jar
But nothing similar to hibernate.cfg.xml. How can I fix it?
hibernate.cfg.xml is your configuration file for Hibernate, where you specify the dialect, connection driver, url, username, password, etc. of the database.
Example:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://host/database</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
</session-factory>
</hibernate-configuration>
Place this file in the Java resources classpath.
For detail information look hibernate.cfg.xml for Oracle or example by mkyong
This is (should be, anyway) an xWiki installation issue. If you are using the WAR file, then when you put the WAR into the tomcat webapps directory, Tomcat normally will expand the WAR into a directory with the same name. In that directory, in the WEB-INF directory, should be the hibernate.cfg.xml file. Typically, here:
/var/lib/tomcat5.5/webapps/xwiki/WEB-INF/hibernate.cfg.xml
If you have configured your Tomcat to run the WAR without expanding it, then the file will be in the WAR at:
WEB-INF/hibernate.cfg.xml
But in this instance you will have to edit the XML file and put it back into the WAR to configure the application.
I had the same problem, and by stracing tomcat I see it looks at the following places, the long one being in the git repository in my openstack gear:
/var/lib/openshift/518f381fe0b8cd1de2000181/git/tomcat.git/hibernate.cfg.xml
/usr/share/java/tomcat7/hibernate.cfg.xml
I cannot see any correlation between these locations and the classpath either given in the command line or in catalina.properties. The git repository is the cwd of tomcat. Putting hibernate.cfg.xml there does work for now. But I believe as soon as openstack guys realize how messy is to put the cwd there, they will move it away.

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/sushi

I have a web application using a jar file(lib) to access a Database.
The jar file when used as a standalone application executes correctly but the webapp received the error:
[01-08-12 13:17:05] - 35266 WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 08001
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/myapp
I have read the answers to similar questions but none of them solve my problem.
I am using Maven, where I have added the dependency for the mysql version I am using, 5.1.21. In fact, I have added it for both the Lib and the Webapp.
Before that, I tried to define in Eclipse a Connectivity Driver Definition bound to the same file, that I had copied to WEB-INF/lib in the eclipse project for my Webapp and with the same parameters that I include below for the persistence.xml file
I am not using any java to configure since I do all the configuration in the persistence.xml file(that I have copied to both META-INF folders, the one for the app(lib) and the one for the webapp. I am using Hibernate (4.1.2) through JPA.
The persistence.xml file is like that:
<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="myappPersistenceUnit"
transaction-type="RESOURCE_LOCAL">
<provider> org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myapp" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
</properties>
</persistence-unit>
</persistence>
the exception that I get is at Runtime, when launching an Http Request to the app, not at the initialization of the webapp, where I haven't seen any errors but this warning appears:
[01-08-12 13:16:39] - 8782 WARN
org.hibernate.engine.jdbc.internal.JdbcServicesImpl - HHH000342:
Could not obtain connection to query metadata : No suitable driver
found for jdbc:mysql://localhost:3306/myapp
I guess this is due to the information being looked up from the database is not necessary at init, and no exception is thrown but the same root cause in both cases.
EDIT:I am using Tomcat 6.0
Any help will be highly appreciated.
It seems that you are missing MySql driver in your project. To solve this you need to add mysql-connector.jar. externally to your project.
Or if you are using a maven project then use the following dependency in your pom.xml file.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.4</version>
</dependency>
You can use a suitable version if needed!!
If you're running your application as a Java Application, Add the JAR file in the Java Build Path, in Eclipse.
Alternatively, I wouldn't put the MSQL jar inside WEB-INF/lib folder of your web project, instead, I would put it in your Application folder library folder, where the Application Server will load your driver and you can access it from the Application Server container.
you should put mysql connector jar with your own code & add jar file with your project.
I also had the same problem some time before, but I solved that issue. There may be different reasons for this exception.
One of them may be that the jar you are adding to your lib folder may be old. Try to find out the latest mysql-connector-jar version and add that to your classpath. It may solve your issue.
I had the same problem with Tomcat 9. Solved by not having the driver jar in both common/lib and WEB-INF/lib (used the former).

Categories

Resources