处理来自SAME VENDOR的多个JDBC驱动程序 [英] Handle multiple JDBC drivers from the SAME VENDOR

查看:119
本文介绍了处理来自SAME VENDOR的多个JDBC驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天遇到了一个大问题。在我当前的项目中,我使用ojdbc6实现Oracle的JDBC进行连接,但我还需要处理例如oracle 8数据库,这对于这个JAR是完全不可能的。
您可以说我应该使用ojdbc14,这对于某些测试是正确的,但我们假设稍后我将需要处理来自同一供应商的2种数据库,但我们知道没有现有的实现对于 BOTH ,我需要加载同时。相同的接口(好吧,不仅仅是相同的接口,相同的类结构,内部只是不同的实现!),相同的URL连接前缀 - > JDBC连接将使用一个驱动程序,但我无法加载其中的多个。那么现在呢?

I came across with a big problem yesterday. In my current project I use ojdbc6 implementation of Oracle's JDBC for a connection, but also I would need to handle for example oracle 8 databases, which is totally impossible with this JAR. You would say that I should use ojdbc14 for example which was true for some tests, but lets assume that later on I will need to handle 2 kind of databases from the same vendor, but we know that there is no existing implementation for BOTH and I need to have those simultaneously loaded. Same interface (and well, not just same interface, same class-structure, just different implementation inside!), same URL connection prefix -> JDBC connection will use one driver, but I cannot load multiple of them. So what now?


  • 我的第一个想法是加载具有不同类加载器的JAR,也许我可以加载相同的包结构相同的班级彼此分开?我真的不这么认为,也许那是我的一个愚蠢的想法。这可能也是一个普遍的问题,以后不仅仅是JDBC驱动程序,所以即使你不能回答我的问题,但你知道这里缺少什么,请告诉我

  • My first idea was to load the JARs with different classloaders, maybe I could load the same package structure with the same classes separated from each other? I don't really think so, maybe that was a silly idea of mine. This could be also a general problem later not with just JDBC drivers, so even if you cannot answer to my question but you know what is lacking here please tell me

即使我可以单独加载相同类名的类实现,在创建连接时如何告诉DriverManager使用EXACT驱动程序而不是根据连接url的前缀找到一个? (我指的是jdbc:oracle:例如,瘦)。

Even if I could do a separate loading of class implementations of the same class names, how I can tell to the DriverManager when creating a connection to use the EXACT driver instead of finding one based on the connection url's prefix? (where I mean jdbc:oracle:thin for example).

我觉得现在总是愚蠢,因为我认为这在Java世界中处理并不是一个完全不同寻常的想法,但我完全不知道如何处理。

I feel like a total dumb now because I think this is not an entirely extraordinary idea to handle in the Java world, BUT I totally don't know how to handle.

感谢你们提前

推荐答案

您实际上有几个选项:


  1. 您可以尝试加载驱动程序不同的班级装载机。如果您的应用程序中只需要纯JDBC,那将会有效。我怀疑你会让Hibernate使用这样的设置。

  1. You can try to load the drivers from different class loaders. That will work if you need only pure JDBC in your application. I doubt that you will get Hibernate to work with such a setup.

最后,你必须运行代码,你需要在这里查看来自两个类加载器的实例,你会得到 ClassCastException s(当从不同的类加载器加载时,两个具有相同完整限定名的类是不同的。)

Eventually, you will have to run code where you will need to see instances from both classloaders and here, you will get ClassCastExceptions (two classes with the same full qualified name are different when they were loaded from different class loaders).

您可以将应用程序拆分为两个。第二个是一个小型服务器,它从原始应用程序获取命令并将这些命令转换为数据库的JDBC。小型服务器与Oracle 8通信,而您的应用程序只与一个数据库通信。

You can split your application into two. The second one would a small server which takes commands from your original app and translates those into JDBC for the database. The small server talks to Oracle 8 while your app only talks to one database.

这种方法可以让您将这两个问题完全分开,但您将无法使用在两个数据库上运行联接。

This approach would allow you to keep the two concerns completely separate but you won't be able to run joins on the two databases.

您可以使用创建数据库链接。这使旧表可见,就像它们是新数据库的一部分一样。你的应用程序只与一个数据库进行通信,而Oracle在内部处理细节。

You can link the old Oracle 8 database in your new database using CREATE DATABASE LINK. That makes the old tables visible as if they were part of the new database. You app only talks to one DB and Oracle handles the details internally.

也许Oracle 8太老了,不能正常工作,但我肯定会试一试。

Maybe Oracle 8 is too old for this to work but I'd definitely give it a try.

Oracle JDBC驱动程序与您可能期望的兼容性更高。当你说这个JAR完全不可能时,你试试吗?我过去使用Oracle 10驱动程序连接到Oracle 7。并非所有功能都受支持,但我可以运行标准查询和更新。

Oracle JDBC drivers are more compatible that you might expect. When you say "which is totally impossible with this JAR", did you try it? I used an Oracle 10 driver to connect to Oracle 7 in the past. Not every feature was supported but I could run the standard queries and updates.

这篇关于处理来自SAME VENDOR的多个JDBC驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆