Java连接Oracle两种方式thin与oci区别

前几天同事跑过来跟我说, 机房中的一台tomcat服务器跟oracle数据库机连接很慢,查看控制台中的hibernate日志, 基本上是一条sql出来要等个1-2秒再出第二条。但同样的程序在他自己机器上的tomcat运行,同样是连那台数据库机器,就快很多,不会出现前面的每 执行1条sql就卡一次壳的情况。
初步分析,我就想到可能是网络原因, 机房两台机器连接不畅通, 程序和机器差的原因基本可以排除, 机房的tomcat机比我们开发机要强多了, 而且程序在他的机器上运行又没有问题。于是我就劝他到机房去检查一下网络状态, 但他一时也无法进入,因为机房的管理人员不在。
过了一会, 他告诉我问题解决了, 把数据库访问的url更换成了oci方式就好了, oci对我来说有些陌生, 我一直是用的thin,也没想过其他连接方式。对于oci我也只能想到oracle 的client中貌似是有oci什么的,当时有其他事情也没管了。

今天有意了解一下区别,先看看thin和oci的url写法上的区别:
jdbc:oracle:thin:@server ip: service
jdbc:oracle:oci:@service
看来oci的还更加简洁,ip可以省掉不写了。

接下来再找找oci和thin的其他区别,发现有如下解释:

引用

Oracle provides four different types of JDBC drivers, for use in different deployment scenarios. The 10.1.0 drivers can access Oracle 8.1.7 and higher. While all Oracle JDBC drivers are similar, some features apply only to JDBC OCI drivers and some apply only to the JDBC Thin driver.

JDBC OCI client-side driver: This is a JDBC Type 2 driver that uses Java native methods to call entrypoints in an underlying C library. That C library, called OCI (Oracle Call Interface), interacts with an Oracle database. The JDBC OCI driver requires an Oracle client installation of the same version as the driver.

The use of native methods makes the JDBC OCI driver platform specific. Oracle supports Solaris, Windows, and many other platforms. This means that the Oracle JDBC OCI driver is not appropriate for Java applets, because it depends on a C library.

Starting from 10.1.0, the JDBC OCI driver is available for install with the OCI Instant Client feature, which does not require a complete Oracle client-installation. Please refer to Oracle Call Interface for more information.

JDBC Thin client-side driver: This is a JDBC Type 4 driver that uses Java to connect directly to Oracle. It implements Oracle’s SQL*Net Net8 and TTC adapters using its own TCP/IP based Java socket implementation. The JDBC Thin driver does not require Oracle client software to be installed, but does require the server to be configured with a TCP/IP listener.

Because it is written entirely in Java, this driver is platform-independent. The JDBC Thin driver can be downloaded into any browser as part of a Java application. (Note that if running in a client browser, that browser must allow the applet to open a Java socket connection back to the server.)

JDBC Thin server-side driver: This is another JDBC Type 4 driver that uses Java to connect directly to Oracle. This driver is used internally within the Oracle database. This driver offers the same functionality as the client-side JDBC Thin driver (above), but runs inside an Oracle database and is used to access remote databases.

Because it is written entirely in Java, this driver is platform-independent. There is no difference in your code between using the Thin driver from a client application or from inside a server.

连接方式有以下几种:

Oralce provides four types of JDBC driver.

Thin Driver, a 100% Java driver for client-side use without an Oracle installation, particularly with applets. The Thin driver type is thin. To connect user scott with password tiger to a database with SID (system identifier) orcl through port 1521 of host myhost, using the Thin driver, you would write :
Connection conn = DriverManager.getConnection
(“jdbc:oracle:thin:@myhost:1521:orcl”, “scott”, “tiger”);

OCI Driver for client-side use with an Oracle client installation. The OCI driver type is oci. To connect user scott with password tiger to a database with SID (system identifier) orcl through port 1521 of host myhost, using the OCI driver, you would write :
Connection conn = DriverManager.getConnection
(“jdbc:oracle:oci:@myhost:1521:orcl”, “scott”, “tiger”);

Note that you can also specify the database by a TNSNAMES entry. You can find the available TNSNAMES entries listed in the file tnsnames.ora on the client computer from which you are connecting. For example, if you want to connect to the database on host myhost as user scott with password tiger that has a TNSNAMES entry of MyHostString, enter:
Connection conn = DriverManager.getConnection
(“jdbc:oracle:oci8:@MyHostString”,“scott”,“tiger”);

If your JDBC client and Oracle server are running on the same machine, the OCI driver can use IPC (InterProcess Communication) to connect to the database instead of a network connection. An IPC connection is much faster than a network connection.
Connection conn = DriverManager.getConnection
(“jdbc:oracle:oci8:@”,“scott”,“tiger”);

Server-Side Thin Driver, which is functionally the same as the client-side Thin driver, but is for code that runs inside an Oracle server and needs to access a remote server, including middle-tier scenarios. The Server-Side Thin driver type is thin and there is no difference in your code between using the Thin driver from a client application or from inside a server.
Server-Side Internal Driver for code that runs inside the target server, that is, inside the Oracle server that it must access. The Server-Side Internal driver type is kprb and it actually runs within a default session. You are already “connected”. Therefore the connection should never be closed.
To access the default connection, write:
DriverManager.getConnection(“jdbc:oracle:kprb:”);
or:
DriverManager.getConnection(“jdbc:default:connection:”);

You can also use the Oracle-specific defaultConnection() method of the OracleDriver class which is generally recommended:
OracleDriver ora = new OracleDriver();
Connection conn = ora.defaultConnection();

Note: You are no longer required to register the OracleDriver class for connecting with the Server-Side Internal driver, although there is no harm in doing so. This is true whether you are using getConnection() or defaultConnection() to make the connection.
Any user name or password you include in the URL string is ignored in connecting to the server default connection. The DriverManager.getConnection() method returns a new Java Connection object every time you call it. Note that although the method is not creating a new physical connection (only a single implicit connection is used), it is returning a new object.
Again, when JDBC code is running inside the target server, the connection is an implicit data channel, not an explicit connection instance as from a client. It should never be closed.

这下基本明白了
1)从使用上来说,oci必须在客户机上安装oracle客户端或才能连接,而thin就不需要,因此从使用上来讲thin还是更加方便,这也是thin比较常见的原因。
2)原理上来看,thin是纯java实现tcp/ip的c/s通讯;而oci方式,客户端通过native java method调用c library访问服务端,而这个c library就是oci(oracle called interface),因此这个oci总是需要随着oracle客户端安装(从oracle10.1.0开始,单独提供OCI Instant Client,不用再完整的安装client)
3)它们分别是不同的驱动类别,oci是二类驱动, thin是四类驱动,但它们在功能上并无差异。
4)虽然很多人说oci的速度快于thin,但找了半天没有找到相关的测试报告。

OCI详情请参考此博客
Oracle官方OCI(Oracle Call Interface)文档翻译

Java连接Oracle两种方式thin与oci区别相关推荐

  1. java制作oracle程序,Java程序操作Oracle两种方式之简单实现

    Java程序操作Oracle两种方式之简单实现 1.通过JDBC-ODBC桥连接Oracle数据库 (1)创建odbc源,在控制面板->管理工具->数据源(odbc)中添加DSN,比如取名 ...

  2. Linux之Ubuntu20.04安装Java JDK8的两种方式

    Linux之Ubuntu20.04远程安装Java JDK8的两种方式 安装openjdk8 更新软件包列表: sudo apt-get update 安装openjdk-8-jdk: sudo ap ...

  3. Java技术分享:升级所安装Java版本的两种方式

    在进行Java开发的时候我们可能会需要升级所安装的Java版本,那么你知道应该如何安装吗?小千今天就来给大家介绍两种方式. 一.卸载掉原本安装的Java,下载最新安装包安装即可. 这个步骤就不介绍了, ...

  4. Java中Http连接的两种方式

    在java中连接http,介绍两种方法,一种是java的HttpUrlConnection,另一种是apacha公司的httpClient,后者是第三方的类库需要从外部,导入,同时这也是第一次使用外部 ...

  5. Java 分页,两种方式的分页,即取即用的代码,不客气

    两种方式,一种是currentPage + pageSize, 一种是limit + offset (limit + offset 这个逻辑太恶心,边缘测试很麻烦) 第一种(currentPage + ...

  6. java实例化字符串两种方式区别

    一:实例化字符串对象的两种方式的区别 这个知识点是面试中的一个经久不衰的问题,.也是一个比较麻烦的问题,对于许多同学来说也是难点,本次课我们会详细的分析.上次课说了创建字符串对象的两种方式:直接赋值( ...

  7. Java练习:两种方式求1+1/2!+1/3!+1/4!+...前20项的和、用for,while,do-while分别实现1+1/2!+1/3!+1/4!+...前20项的和

    Java两种方式求 1+1/2!+1/3!+1/4!+... 前20项的和: 感叹号 !是阶乘的意思,如 2! .3! .4! 分别是:1*2 . 1*2*3 .1*2*3*4  第一种算法,如下代码 ...

  8. Java异常处理的两种方式

    异常处理方式一:在当前方法中直接用try-catch处理 异常处理方式二:在当前方法中不处理,throws 异常抛给调用者处理 一. try-catch的方式就是捕获异常. try:该代码块中编写可能 ...

  9. erl0007 - erlang 远程节点连接的两种方式

    启动连接:erl -setcookie abc -name xxx@192.168.x.x -remsh xxx@192.168.x.y 退出:ctrl + g,q 参考:http://www.cnb ...

最新文章

  1. ios 裁剪圆形头像_iOS开发笔记:实现圆形头像
  2. UOJ46. 【清华集训2014】玄学
  3. 用命令行非交互改密码
  4. python3 判断字符串是否包含指定字符
  5. JS拖拽,移动与拉伸
  6. 双十一,单身狗除了买买买,还能做什么?
  7. ThinkPHP系统流程
  8. java.net.ConnectException: Connection refused: no further information
  9. 第7篇 WPF C# 怎样定义类及其接口
  10. 运动目标跟踪(十)--CSK跟踪
  11. Arduino学习笔记14
  12. 关于LED限流电阻计算的那些事儿
  13. AABB和OBB包围盒简介
  14. android手机传文件,Android手机间如何高速互传文件?
  15. 使用JSON数据格式模拟股票实时信息
  16. 使用throttlestop超频,解除锁频和温控
  17. 李航《统计学习方法》第2版 第2章课后习题答案
  18. 爬虫-Python入门
  19. 影视后期学哪种计算机语言,小白想入行影视后期,首先要学哪款软件?
  20. 鸿蒙OS 2.0 开源网址 源码仓库

热门文章

  1. 排序算法之一——冒泡排序
  2. Mac简单的上手指南
  3. WPF教程(二十)密码框
  4. c语言编程温度传感器代码,单片机控制的温度传感器C语言程序代码(WORD档).doc
  5. 用python发布一条微博
  6. c语言 字母消消乐,消消乐(C语言版)
  7. 我是如何向别人提问题的?
  8. 金三银四已消失?如何快速斩获春招offer
  9. 不调用库函数实现strcpy
  10. 关于Web软件的界面设计——《Web软件用户界面设计指南》