Use different version of derby.jar then the one included with WebSphere? - java

I’m trying to use an imbedded Derby database from within a war file deployed on WebSphere for Z/OS. When I use code like the following to create a database I wind up creating a derby version 10.3 database using the derby.jar included with WebSphere server 7.0 instead of the version 10.5 derby.jar I include in my WEB-INF/lib directory.
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:derby:testDB;create=true");
DatabaseMetaData dbmd = conn.getMetaData();
String productName = dbmd.getDatabaseProductName();
String productVersion = dbmd.getDatabaseProductVersion();
How can I force WebSphere to use the org.apache.derby.jdbc.EmbeddedDriver class included in the derby.jar file I package in my war file?

I think you'll need to switch the default classloader to look into the web app first.
Specify PARENT_LAST as documented here : See "Setting classloaders in a Web module"
But this is a risky option since it can affect all other classes which might be duplicated between app and war.

In a J2EE environment, it's better to let the server manage connections, and it's usually not a good idea to put JDBC providers in your WAR.
New in WAS 7.0 is a feature known as "isolated resource providers" that allows you to configure multiple versions of Derby to run in parallel in the same JVM.

Related

Get error java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost;databaseName=ABC;integratedSecurity=true on Production

I created a Web Application using Java, JSP,Tomcat via Eclipse.
When running the Application on my Windows development Env, it works fine. database connection is fine (sqljdbc42.jar in Lib via Eclipse) The test db is on my Window development PC
Link: http://localhost:8080/App/
To test the db connection on Production is working right, I have a Java file (not related to my project, but using same db driver and url. It's just for test DB connection on production), it connects to db which is located on production correctly.
However, after I upload my war file (from local development environment) to Tomcat WebApps folder, I got the error in title, the db on production is not connected successfully.
I use the following drivers and url for db connection
drivers=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost;databaseName=ABC;integratedSecurity=true
In the above url string, I have tried localhost as above, also tried production IP address, Server name, localhost:1433, localhost\\MSSQLSERVER, all get the same error
I have below in ClassPath in production site:
C:\sqljdbc42.jar;C:\microsoft-mssqlserver.jar;C:\Program Files\Java\jdk1.8.0_201\bin;C:\Program Files\Java\jre1.8.0_201\bin;.
Thank you in advance.
You should check the classloader paths for tomcat server. Regarding the docs, your server will hope finding sqlserver driver jar (sqljdbc42.jar) in the following places:
Bootstrap classes of your JVM ($JAVA_HOME/jre/lib/ext)
/WEB-INF/classes of your web application
/WEB-INF/lib/*.jar of your web application
System class loader classes (described through CLASSPATH environment variable)
Common class loader classes ($CATALINA_BASE/lib and $CATALINA_HOME/lib)
So, best options you can try are:
copying the jar to $JAVA_HOME/jre/lib/ext (#1)
or configuring your build so that, the jar is copied in /WEB-INF/lib/ (#3)
Number#2 is suitable for java classes; does not fit your case.
Number#4 and #5 are more likely for the use of tomcat's own structure, but still it is perfectly legal if you want to put the jar in.
Further Edit: For some reason, there are cases Tomcat may prevent sqlserver driver for the sake of preventing memory leaks. So, if you're sure the jar is on the classpath by applying the solutions mentioned in beforehand this post; you may also try to check with adding the following to server/.xml to disable that leak check:
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" driverManagerProtection="false" />

JDBC driver not found (servlet, DAO, mariaDB) [duplicate]

I developer a web application using Java. When I deploy it to my application server (Jetty, Tomcat, JBoss, GlassFish, etc.) throws an error. I can see this error message in the stacktrace:
java.lang.ClassNotFoundException
Or
java.lang.NoClassDefFoundError
What does this mean and how can I fix it?
What does this mean?
First, let's see the meaning of java.lang.ClassNotFoundException:
Thrown when an application tries to load in a class through its string name using:
The forName method in class Class.
The findSystemClass method in class ClassLoader.
The loadClass method in class ClassLoader.
but no definition for the class with the specified name could be found.
Usually, this happens when trying to open a connection manually in this form:
String jdbcDriver = "...'; //name of your driver
Class.forName(jdbcDriver);
Or when you refer to a class that belongs to an external library and strangely this class cannot be loaded when the application server tries to deploy the application.
Let's see the meaning of java.lang.NoClassDefFoundError (emphasis mine):
Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.
The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.
The last part says it all: the class existed at compile time i.e. when I compiled the application through my IDE, but it is not available at runtime i.e. when the application is deployed.
how can I fix it?
In Java web applications, all third party libraries used by your application must go in WEB-INF/lib folder. Make sure that all the necessary libraries (jars) are placed there. You can check this easily:
- <webapp folder>
- WEB-INF
- lib
+ jar1
+ jar2
+ ...
- META-INF
- <rest of your folders>
This problem usually arises for JDBC connectivity jars (MySQL, Derby, MSSQL, Oracle, etc.) or web MVC frameworks libraries like JSF or Spring MVC.
Take into account that some third party libraries rely on other third party libraries, so you have to add all of them in WEB-INF/lib in order to make the application work. A good example of this is RichFaces 4 libraries, where you have to download and add the external libraries manually.
Note for Maven users: you should not experience these problems unless you have set the libraries as provided, test or system. If set to provided, you're responsible to add the libraries somewhere in the classpath. You can find more info about the dependency scopes here: Introduction to the Dependency Mechanism
In case the library must be shared among several applications that will be deployed on your application server e.g. MySQL connector for two applications, there's another alternative. Instead of deploying two war files each with their own MySQL connector library, place this library in the common library folder of the server application, this will enable the library to be in the classpath of all the deployed applications.
This folder vary from application server.
Tomcat 7/8: <tomcat_home>/lib
JBoss 7/Wildfly: <jboss_home>/standalone/lib
The class must exist under WEB-INF/classes or be inside a .jar file under WEB-INF/lib. Make sure it does.
Same problem happen with me.
Might be possible one of your libraries are using some classes internal which is not available
in your lib or maven dependency pom.xml.
Thats means you have analyze your error logs and identify these classes and then import all dependencies in maven or lib folder.
I have fixed this error by the same way.
because some of my libraries are using activation.jar and json.jar internally.

using JDBC within dynamic web application

I have a dynamic web application using struts2 that works perfectly.
I have a JDBC application that works perfectly (all it does is print a table out in the console).
After I put the two together all that was left was to add authentication to the run configuration and I'd be done...
I've already put the sqljdbc_auth.dll to the WEB-INF/lib
I've tried to add this line to Tomcat v7.0 Server at localhost
-Djava.library.path="C:\Program Files (x86)\jdbc\sqljdbc_4.0\enu\auth\x86"
but it doesn't do anything
This driver is not configured for integrated authentication.
ClientConnectionId:6495...
It's the line that got my JDBC application working just fine.
Would there be some syntax error I'm missing or another method for me to pass an argument that I'm not aware of?
It looks like you are using the Microsoft SQL Server Driver which is pure java (type 4) so you dont need to mess with java.library.path. Set the Classpath as described here
Alternatively include the jdbc driver in WEB-INF/lib within your WAR file.

Get Information about the used Libraries of application

Is there a chance to get an information about libraries used in my java application?
Example: If I deploy my application on glassfish, the glassfish has it's own libraries and my application has own. If I use a newer version of a lib as the glassfish has, I want to test/log that my lib is used.
The reflection API lets you find some information about classes loaded into the application. For example assuming the library that includes SomeClass is loaded from a .jar file that includes version information you can use:
Package libPackage = SomeClass.class.getPackage();
String libVersion = libPackage.getSpecificationVersion();
If the .jar file does not include version information you can still find out its name with:
URL classLocation = SomeClass.class.getProtectionDomain().getCodeSource().getLocation();

drivers loaded in apache-tomcat-6.0.36/lib but still get No suitable driver found

I am learning web services in eclipse helios with axis 2 and apache tomcat. I have two dynamic web projects with java classes that successfully connect to two databases when I run them as simple java classes in eclipse. (I have added the external jars to the external build path for the project). But when I run either on the server, I get an error: No suitable driver found. I know that I need to load the necessary drivers into apache-tomcat-6.0.36/lib and I have done so (and re-started the server). (see No suitable driver found).
I use this statement to create the driver in my Java class:
Class.forName("org.postgresql.Driver"); //throws class not found exception w/message "com.postgresql.jdbc.Driver"
conn = DriverManager.getConnection(url, user, password);
Why doesn't apache "see" the .jar drivers in its /lib folder? Some older tutorials say to put the .jars in common/lib--but I don't see that folder in my apache tomcat directory structure. What can I do to debug this problem?
Depending on the version of Java (and thus, the JDBC version) you use, you might need to call Class.forName() before calling DriverManager.getConnection(...). This forces the JVM to load the class, so that JDBC knows the class is a driver for your type of connection. Without that, JDBC knows no driver for your type of database and thus spits out the "No suitable driver found".
If you're running your class in Eclipse using Java 7 (and thus JDBC 4.0) the drivers that are found in your class path are automatically loaded. With Java versions before 7 (and thus JDBC before 4.0), you have to register your driver by hand, like explained. See the JDBC tutorial for details.
If this line is uncommented in ${CATALINA_HOME}/conf/server.xml:
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
Change it to:
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" driverManagerProtection="false" />
This returns Tomcat to pre-6.0.35 functionality, where it correctly registered the type 4 JDBC drivers in ${CATALINA_HOME}/lib.

Categories