Saturday, December 29, 2012

JDBC Thin Driver and OCI Driver (Include 64bit solaris resolution)

JDBC Thin Driver and OCI Driver (Include 64bit solaris resolution)
Oracle recommends that
"Consider the following when choosing a JDBC driver for your application or applet:
¦ In general, unless you need OCI-specific features, such as support for non-TCP/IP networks, use the JDBC Thin driver.”
And the Thin Driver has a slight better performance over OCI Driver.
However, to use Thin Driver, the ip or hostname has to be destinated, which will bring extra effort to our software release procedure.
Anyway, we have no other choice then to use OCI Driver.
For example:
import java.sql.*;
import oracle.jdbc.pool.OracleDataSource;
 
class DBTest {
        public static void main(String args[]) throws SQLException {
                OracleDataSource ods = new OracleDataSource();
                //ods.setURL("jdbc:oracle:thin:@192.168.2.1:1521:orcl");
                ods.setURL("jdbc:oracle:oci:@cnr04");
                ods.setUser("xxx");
                ods.setPassword("xxx");
                Connection conn = ods.getConnection();
                // Create Oracle DatabaseMetaData object
                DatabaseMetaData meta = conn.getMetaData();
                // gets driver info:
                System.out.println("JDBC driver version is " + meta.getDriverVersion());
                conn.close();
        }
}
Before run the test programme, on Sun Solaris, set LD_LIBRARY_PATH as follows:
$ORACLE_HOME/lib
or java.lang.UnsatisfiedLinkError: no ocijdbc10 error would be reported.
It is weired that after the LD_LIBRARY_PATH is set, java.lang.UnsatisfiedLinkError:wrong ELF class: ELFCLASS64 error reported.
This error arise on 64bit solaris since $ORACLE_HOME/lib is based on 64bit ("isainfo -kv" )
While java is based on 32bit on default. (use java -d64 -version check java version)
To fix this error, two options are available:
1. Set LD_LIBRARY_PATH as $ORACLE_HOME/lib32
2. Add -d64 to let java use a 64-bit data model. (This option is not supported on Windows until version 1.6.0_06: Unrecognized option). The full command example is:
java -cp .:lib/ojdbc14.jar -DLD_LIBRARY_PATH=$ORACLE_HOME/lib -d64 DBTest
Below is the description of d32 and d64 from Sun's website (http://java.sun.com/j2se/1.4.2/docs/tooldocs/solaris/java.html):
d32 -d64 Specifies whether the program is to be run in a 32-bit or 64-bit environment. On Solaris these correspond to the ILP32 and LP64 data models, respectively. The -d64 option may only be used on 64-bit Solaris systems.
Currently only the Java HotSpot Server VM supports 64-bit operation, and the "-server" option is implicit with the use of -d64. This is subject to change in a future release.
If neither -d32 nor -d64 is specified, the default is to run in a 32-bit environment. This is subject to change in a future release.

No comments:

Post a Comment