How the hibernate selects ojdbc jars if multiple jars are available? - java

We have 4 ojdbc jars namely ojdbc14.jar, ojdbc6.jar, ojdbc7.jar and ojdbc8.jar in the "lib" folder of the java application.
And we have declared the necessary configurations for the driver in XML file as shown below,
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
In the above options, Which ojdbc jar does hibernate choose? whether it will select the first jar or will select one of the ojdbc jars at random.
And based on what logic does Hibernate select the appropriate ojdbc jar file?
Thanks in advance.

Related

No suitable driver found when exporting to a JAR-file?

I am trying to get my project into a JAR so I can run it as a CLI.
I have two JDBC connectors that I am using, one for MySQL one for PostgreSQL. Both are located in the same directory and work fine if I run them in the IDE.
When I create the JAR the MySQL connector still works fine, however when trying to establish a connection to PostgreSQL the following error appears.
What really irritates me is that the connector seems to be included in the build of the jar.
Both the MySQL connector and PostgreSQL connector are listed in the build.
How can I go about fixing this?
The problem is that all JDBC-4-compliant JDBC drivers contain a file /META-INF/services/java.sql.Driver that lists the java.sql.Driver implementations in the JAR files. This is used by the java.sql.DriverManager to load the available JDBC drivers.
The process you used for merging apparently doesn't merge the different files from the drivers into a single file, so it only has the content of one of the drivers. As a result, the other driver isn't loaded automatically.
Possible solutions:
Don't merge JAR files into a single JAR, but instead use the Class-Path attribute of META-INF/MANIFEST.MF to specify the JARs you use, and execute your program with java -jar your.jar
Make sure META-INF/services/java.sql.Driver is merged correctly (or maybe provide your own), depending on how you merge, there might be an option to configure which files need to be merged
Explicitly load the drivers using Class.forName("com.mysql.cj.jdbc.Driver") and Class.forName("org.postgresql.Driver") (do it for both to prevent problems if order of merging files changes, and the other file wins)

Glassfish 3.1.2 doesn't contain javax.persistence jar

I'm trying to work with Eclipse Mars 2 and Glassfish 3.1.2, but when I use EclipseLink 2.1 to create the entitites from tables, javax.persistence is not found in the buildpath.
Is this the normal behaviour?
I created the Oracle db connection with eclipse
I installed JEE 6 SDK with Glassfish 3
I started the glassfish server and configured the data source and connection pool.
I created a JPA project and created the entities from the db connection tables.
Something I find curious is that the jee.jar that is inside glassfish/lib only contains a pom.xml
Isn't Glassfish supposed to contain all the jars needed to develop with the jee standard?
Is my project meant to be a Maven project in order to automatically download this jars? Could I fix this by configuring my project as a Maven project?
Isn't Glassfish supposed to contain all the jars needed to develop with the jee standard?
Yes, and it contains all the jars needed but in an indirect way. Even though it is a single jar file with only one file, namely, MANIFEST.MF and a directory maven which contains the pom file you mentioned.
But if you open the MANIFEST.MF file you'll see that it is pointing to a lot of jar files in the Class-Path: entry which you need to be able develop JavaEE applications.
So everything is there and you only need to add this file into your dependency to be able to compile your JavaEE (Servlets, JSF, EJB, JPA, ...) classes and deploy on the server. But this (javaee.jar) must be a compile time dependency, i.e., should not be packaged with your application as the jars are already contained in the server.
Is my project meant to be a Maven project in order to automatically download this jars?
No. Your project can be but should not be a maven project. Let us say, for example, you have an Eclipse project where you want to develop an EJB application. Just right click on the project
go to the Properties -> Build Path page and change to Libraries tab
click on Add External Jars ...
go to the Glassfish lib directory and add the javaee.jar, and you are done.
now you can use all the JavaEE annotations and classes.

JDBC ClassNotFound even with MySQL library

So I am trying to connect to my server, I had it working before but now I moved to a different machine it's not working as you can see below.
I have added the MySQL driver into my path and I'm not sure what the problem could be.
Any ideas?
Thanks.
You indeed have defined that MySQL connector is a library for your project, but you also need to add it as a module dependency on the Modules tab in Project Structure.
If you haven't dependency management such as maven (gradle, etc), create lib folder in project root folder. Move jar to lib folder and right click on it and select "Add as library".
This should help you.

jdbc driver not found when execute from exported Jar

I have a Java applet, it works correctly when executed from eclipe, but I want to export it to a Jar and use it. but when I do that, i get jdbc driver not found, it seems like when exporting, jaybird is not exported.
For exporting I use eclipse export and choose Java/JAR File, in build path i have jaybird mark to export.
Any suggestions? Thanks in advance
Use Fat Jar to build runnable JARs that contain all dependent libs
You are most likely missing the files from the META-INF folder of the Jaybird jar file. These files are required for Jaybird to work. Another possibility is that you are missing one of the required dependencies of Jaybird (connector-api-1.5.jar, for Jaybird 2.2 or mini-j2ee.jar for earlier versions).
Even if you get this to work though, you will most likely experience an error later on, as Jaybird wasn't developed with support for applets in mind, see http://tracker.firebirdsql.org/browse/JDBC-254 and NoClassDefFoundError with jdbc applet
BTW: Why don't you just use the jar as is. IMHO creating fat jars including all your dependencies is ugly and inflexible.
I finally get the answer, I had to sign the jaybird jar also because that jar was doing read/write operation in HDD
By using Eclipse you can simply solve the problem.
By going to Eclipse -> File -> Export -> Runnable JAR file and selecting Extract required libraries into generated JAR option, Eclipse will extract required libraries beside your project and creates the required MANIFEST.MF file for you and then will pack them all together in your JAR file.

Eclipse: Exported library fail to instantiate oracle driver ojdbc.jar

I have a library that contains functionality to connect to on oracle database. When I export this library (as a JAR) and use it in the main project, it gives an exception when loading the driver with class.forname. It obviously cannot find the ojdbc driver. I included this driver in the build path and as exported library.
I tested and used the driver directly from the main project, and it work, it connects to oracle db.
Thanks.
The problem is that your driver is a jar file, and when you export the app as a jar file, that driver will be a jar file in a jar file. For that scenario you either need a special classloader or put the driver jar file in the classpath of your main program.
Explore your exportedjar by using WinRar and check if it contains a jar under jar.

Categories