permaculture playing cards*
The moose likes JDBC and the fly likes Connecting to multiple databases using different jdbc drivers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connecting to multiple databases using different jdbc drivers" Watch "Connecting to multiple databases using different jdbc drivers" New topic
Author

Connecting to multiple databases using different jdbc drivers

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

I have a requirement to write a daemon based process (Not web based) that will connect to an Oracle 10G database, read some data from it and then connect to a SQL Server database and write the data to a table.

Sounds simple enough but i have a couple of queries about this.

* i will need to have two jdbc drivers, i.e. one for connecting to the Oracle database and the other for connecting to the sql server database. The sql server jdbc driver is the jtds jdbc driver (http://jtds.sourceforge.net/) and for Oracle i will be using the standard oracle jdbc driver. Am i likely to come across any problems with both drivers available in the classpath together? Is there a way to achieve this with only one driver? I had a look at hibernate and decided that it would be overkill as all i want to do is extract data and insert it into another database so hibernate wont provide any advantage in this scenario.

* My guess is that all i need is a ConnectionManager class to manage the connections and a client DAO class that would call the relevant method to get the connection it needs depending on whether it is reading from Oracle or writing to SQL Server. Is this a reasonable approach or is there a better design/pattern for this?

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41613
    
  55
Am i likely to come across any problems with both drivers available in the classpath together?

No

Is there a way to achieve this with only one driver?

No

I had a look at hibernate and decided that it would be overkill as all i want to do is extract data and insert it into another database

Most likely true.

My guess is that all i need is a ConnectionManager class to manage the connections and a client DAO class that would call the relevant method to get the connection it needs depending on whether it is reading from Oracle or writing to SQL Server. Is this a reasonable approach or is there a better design/pattern for this?

If this task is small enough that you think using ORM would be overkill, then anything fancier than this is probably overkill as well.


Ping & DNS - my free Android networking tools app
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Ok i have tried to put together a quick design solution. See image below



The problem i think i am having is how to commit. Here is the flow of processing

- InvoiceBD gets an Oracle connection from the factory class and calls InvoiceUploadDAO.readData passing it the Oracle connection object.
- InvoiceBD get a SQL Server connection from the factory class and calls InvoiceUploadDAO.writeData passing it the SQL Server connection object.
- InvoiceBD reuses the Oracle connection to call InvoiceUploadDAO.update status to 'Complete' set status on the Oracle database.

InvoiceBD commits the Oracle connection.
InvoiceBD commits the SQL Server connection.

Or if something goes wrong both connection objects are rolled back.

Does that sound like a possible approach? I cant seem to workout how to resolve a situation where the first commit succeeds but the second fails.

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41613
    
  55
Transactions involving multiple databases can't be handled with regular JDBC transactions; you need to use distributed transactions (so-called "XA" transactions). You need to check whether the databases drivers you're using support distributed transactions.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Ulf Dittmer wrote:Transactions involving multiple databases can't be handled with regular JDBC transactions; you need to use distributed transactions (so-called "XA" transactions). You need to check whether the databases drivers you're using support distributed transactions.


Hi yes i read up about XA transactions and thought that it was quite complicated and overkill for what i am trying to do. The process i am writing will be written so that if SQL Server commit fails the Oracle commit will not take place. This means the process can be re-run at any time because it did not complete on the previous attempt. The Oracle commit will be done last and if this does not happen the whole transaction will be treated as a failed transaction.

if the SQL server commit succeds but the oracle commit fails, the transaction will still be marked as failed so the process can be rerun again. When it is rerun, the data on the sql server is deleted and rewritten.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41613
    
  55
If it's OK to commit something on SQLServer even if the commit fails on Oracle, then I guess regular TX will work.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Ulf Dittmer wrote:If it's OK to commit something on SQLServer even if the commit fails on Oracle, then I guess regular TX will work.


Yes the reason being that the commit on Oracle signifies success. If the Oracle commit fails the process will not be successfull. When it is rerun again the first thing it does is truncate the data on the SQL server tables and populate it with fresh data.

Thanks


 
Don't get me started about those stupid light bulbs.
 
subject: Connecting to multiple databases using different jdbc drivers