Java JDBC - How to connect to Oracle using Service Name instead of SID

前端 未结 8 962
醉话见心
醉话见心 2020-11-22 15:14

I have a Java application that uses JDBC (via JPA) that was connecting to a development database using hostname, port and Oracle SID, like this:

jdbc:oracle:

8条回答
  •  北海茫月
    2020-11-22 15:21

    So there are two easy ways to make this work. The solution posted by Bert F works fine if you don't need to supply any other special Oracle-specific connection properties. The format for that is:

    jdbc:oracle:thin:@//HOSTNAME:PORT/SERVICENAME
    

    However, if you need to supply other Oracle-specific connection properties then you need to use the long TNSNAMES style. I had to do this recently to enable Oracle shared connections (where the server does its own connection pooling). The TNS format is:

    jdbc:oracle:thin:@(description=(address=(host=HOSTNAME)(protocol=tcp)(port=PORT))(connect_data=(service_name=SERVICENAME)(server=SHARED)))
    

    If you're familiar with the Oracle TNSNAMES file format, then this should look familiar to you. If not then just Google it for the details.

提交回复
热议问题