Without calling Class.forName("com.mysql.jdbc.Driver") run program - jdbc

Without load Driver Class.forName("com.mysql.jdbc.Driver") my program
work is fine... How...?

This is explained in the javadoc of DriverManager
The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver. This file contains the name of the JDBC drivers implementation of java.sql.Driver. For example, to load the my.sql.Driver class, the META-INF/services/java.sql.Driver file would contain the entry:
my.sql.Driver
Applications no longer need to explicitly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.
When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly using the same classloader as the current applet or application.

Related

How to use p6spy for standalone application that limits its JDBC drivers?

I have a standalone 3rd party application working with Oracle database and I need to troubleshoot its DB queries. And I don't have access to its source code (nor desire to decompile it :-) ).
Its DB connection configuration has several separate parameters:
Driver: oracle.jdbc.OracleDriver
JDBC URL: jdbc:oracle:thin:#localhost:1521:orcl
User name & password
But I can't change driver as application is checking for a list of supported drivers and just refuse to start if I put com.p6spy.engine.spy.P6SpyDriver into the driver parameter.
So can p6spy still be used in this case? If not, is there any other way to trace application DB access from application's end (I'm aware of Oracle tracing, that would be my next step if this won't work)?
Many thanks!
You can use P6Spy in Datasource way

Does a java application that uses JDBC 4 driver type requires db2 binding

Our java applications will access legacy mainframe db2 databases.
I remember in my previous projects, C++ applications required db2 binding before deployment.
In current projects, all mainframe applications, Cobol packages are also required to do db2 binding.
Does a java application that uses jdbc4 driver needs db2 binding too?
If you are using standard JDBC you are creating dynamic SQL (PrepareStatement) that do not requires binding in DB2 side.
However, if you use SQLj, you will need to 'precompile' that code to generate you .java files and another file to bind in the database.
It does not matter if you are connecting to a mainframe (system z or i) or to a DB2 LUW. The concept is the same for all platforms as DB2 is DB2.
SQLj is not very popular, however is very powerful to tune your queries and improve the data access, however, as you used to do in C, the code has to be developped in more phases, and you have to rebind each time the access plan has to be modified (new statisques, security, etc.)
SQLj is very easy to use from Data Studio, and SQLj from DB2 is not exactly the same from Oracle.
The JDBC type IV driver, supplied by IBM, will handle everything you need to do.
The driver maps Java objects into DB2 appropriately.
I don't know what "binding" means in this context. Java is not C++.
If your DB2 version supports Dynamic Statement Caching and enabled (consult your DBA’s), you can use JDBC Type-4 driver to access mainframe DB2 database (use prepare statement) without binding.
DB2 will generate access paths and store it in first request into cache. Otherwise, you need use SQLJ like technologies and bind them.

What is difference between oracle.jdbc.xa.client.OracleXADataSource and oracle.jdbc.pool.OracleDataSource

I am trying to understand the difference between XA vs Non XA JDBC datasource. Also how do I know which type and version of JDBC dtriver is used. I am currently on 10.3 weblogic and trying some tet to kill long running queries using setQueryTimeout, which isnt seem to be reliable with OracleXADataSource as it is only working the first time and not always.
Sorry for this basic question but I am new to Weblogic Datasource configuration
Thanks
XA jdbc drivers are used to implement two-phase commit, meaning the two remote resources are part of the same transaction. Java specifies an implementation of this via JTA. A good reading is e.g. http://www.javaworld.com/javaworld/jw-07-2000/jw-0714-transaction.html; if you google for 'xa jdbc driver' you'll find plenty more info.
You should not use the XA driver if not necessary. I remember reading that there are some problems with them.
To identify JDBC driver your WLS is using, go to the <domain_dir>/config/jdbc and open the data souce file, check the driver-name value in the file.
To identify the Driver version, check from which .jar is the driver being loaded (run the WLS with -verbose:class)- the name of the jar will contain the version number. Also, you can use java -jar my-jdbc-file.jar which will print the driver version. The OJDBC drivers are usually stored in a file named ojdbc6.jar or ojdbc7.jar, etc.

Loading a jdbc driver in a java class

In a simple jdbc example , when we load a driver through a java class, the practise is to use Class.forName("com.mysql.jdbc.Driver").newInstance();
Why we do not use new com.mysql.jdbc.Driver();?
This piece of code isn't really intended to create an instance of the driver but to ensure the driver class is loaded by the class loader. Nowadays this is no longer needed, see:
What is the actual use of Class.forName("oracle.jdbc.driver.OracleDriver") while connecting to a database?
JDBC Class.forName vs DriverManager.registerDriver
Loading JDBC driver
What is the purpose of 'Class.forName("MY_JDBC_DRIVER")'?
JDBC connection- Class.forName vs Class.forName().newInstance?

custom JDBC driver for SAP Business Objects

I'm writting custom JDBC driver for my datasource. I'd like to use it with Business Objects product from SAP. My driver works fine in Squirell SQL, but when installed in Business Objects (specifically Universe Designer) I'm unable to fetch table contents. I've enabled TRACE for whole Business Objects instance and the problem's log looks like this:
dictionary_thread::Columns : CS:Unexpected behavior
com.businessobjects.connectionserver.RDBMSDictionary}.Columns: [com.sap.connectivity.cs.core.CSError: Unexpected behavior
As for debugging my JDBC driver, I've been using log4jdbc as well as custom logging of method calls and return values (via aspects) and I don't see any errors, exceptions coming from my driver.
Perhaps someone knows a solution for this, or knows how to debug this further?
It seems obvious that my driver is not fully JDBC specs comformant, so any Test Suite which could analyze errors in my driver would also be of use.
The JDBC API Test Suite 1.3.1 is quite outdated but it may help you diagnose your issue, at least for JDBC 2.0 API: Download is no longer available, remains an old Documentation
According to JSR221 for JDBC4 you may ask for JavaSE TCK (JCK) sources at no charge but you have to follow this process. You will have to accept OpenJDK TCK license.

Resources