科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道关于JDBC客户端如何连接ORACLE数据库RAC的负载均衡

关于JDBC客户端如何连接ORACLE数据库RAC的负载均衡

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

这篇文章主要介绍如何使用jdbc配置连接数据库(oracle的RAC配置的数据库)达到负载均衡的情况.该例子是以2个NODE的情况说明,希望对大家有所帮助. 我的问题是我需要设置oracle9.2.x的thin客户端连接到oracle

作者:中国IT实验室 来源:中国IT实验室 2007年10月6日

关键字: JDBC 数据库 ORACLE

  • 评论
  • 分享微博
  • 分享邮件

    这篇文章主要介绍如何使用jdbc配置连接数据库(oracle的RAC配置的数据库)达到负载均衡的情况.该例子是以2个NODE的情况说明,希望对大家有所帮助.

    我的问题是我需要设置oracle9.2.x的thin客户端连接到oracle的RAC环境上.

    注:这样连接可以通过RAC自动平衡负载.

    --原文如下:

Hi Tom,

I couldn't find this information easily on the net.  So I'm submitting it here
and hope you make it available for anyone else looking for this information.

My problem was I needed to configure the Oracle 9.2.x thin driver (type IV) to
connect to an Oracle Real Application Cluster (RAC) environment.

For example, assuming you have a database called RAC_DB with two nodes - node1
and node2.

You would need to configure your tnsnames.ora with the following information in
the following way:

RAC_DB =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = node1)(PORT = 1521))
      (ADDRESS = (PROTOCOL = TCP)(HOST = node2)(PORT = 1521))
      (LOAD_BALANCE = yes)
      (FAILOVER = on)
    )
    (CONNECT_DATA =
      (SERVICE_NAME = RAC_DB)
      (FAILOVER_MODE = (TYPE = SELECT)
                       (METHOD = BASIC)
                       (RETRIES = 20)
                       (DELAY = 10)
      )
    )
  )

Now if you start a SQL*PLUS session, then you should see a connection on the
node1 instance.  If you start another SQL*PLUS session, then Oracle should
connect you to the node2 instance (automatic load balancing).  It's actually
pretty cool to see the first time.

To pass this same information to the Oracle's JDBC thin driver, you essentially
concatenate a shorten version of the tnsnames information above and pass this to
the driver.

  String userid = "scott";
  String password = "tiger";

  String tnsnames_info =
    "(DESCRIPTION=(ADDRESS_LIST=" +
    "(ADDRESS=(PROTOCOL=TCP)(HOST=node1)(PORT=1521))" +
    "(ADDRESS=(PROTOCOL=TCP)(HOST=node2)(PORT=1521))" +
    "(LOAD_BALANCE=yes)(FAILOVER=on))" +
    "(CONNECT_DATA=(SERVICE_NAME=rac_db)))" ;

  String url = "jdbc:oracle:thin:@" + tnsnames_info;

  DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
  Connection dbConnection = DriverManager.getConnection(url, userid, password);

That's it.  If your application creates multiple connection to the database,
then you should see these connections load balance across the two instances.

One last note, Oracle only supports connection to a RAC configuration with the
9i drivers, so you should try to get the latest 9.2.0.4 Oracle JDBC thin driver.

HTH,
-Peter

--------------------------------------------------------------------------------
and we said...

you made it really hard.  you just needed the service!  the load balancing and
all could be/should be setup on the listener side!

you have one listener, both databases register with it as a service.


that would be another option.

   Reviews

--------------------------------------------------------------------------------
I think we tried that and it didn't work...  September 28, 2003
Reviewer:  Peter Tran  from Houston, TX USA

Hi Tom,

I'm pretty sure we tried that but it didn't work with the thin driver.  That
approach will work if you use the OCI driver, but not with the thin driver.

Please send me an example of what you mean or what files I should configure to
test it out.

I'm always opened to easier options.

Thanks,
-Peter

Followup:
you need to set up mts and a single listener, thats it.

pmon on each of the rac instances will tell the listener about the load and away
it goes.  you might not see the round robin right off (both are "not yet
loaded") so it'll be an unbalanced load balance initially but as the system
ramps, it'll balance out.

--------------------------------------------------------------------------------
Unknown territory...  September 28, 2003
Reviewer:  Peter Tran  from Houston, TX USA

I'm sorry, but I really lost you with that last recommendation.  Rather than
frustrate you with my ignorance, can you recommend the Oracle documentation that
I should read to brush up on this information?

I'll read this first and come back with questions if I'm still lost.

For example, I don't understand why you want me to setup the database as MTS.
Why can't I use dedicated server mode?

Thanks for the quick response.

-Peter

Followup:
in order for a single listener to service many instances -- on different
machines -- the listener must be servicing shared server connections -- the
listener cannot "fork/exec" a dedicated server since the listener may well not
be running on the machine the instance is on.  it needs to know dispatcher
addresses to redirect the client request to.

http://download-west.oracle.com/docs/cd/B10501_01/network.920/a96580/architec.htm#1045787

it is just inherit in the architecture.

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    闂傚倸鍊搁崐椋庢閿熺姴鐭楅幖娣妼缁愭鏌¢崶鈺佷汗闁哄閰i弻鏇$疀鐎n亞浠炬繝娈垮灠閵堟悂寮婚弴锛勭杸閻庯綆浜栭崑鎾诲冀椤撱劎绋忛梺璺ㄥ櫐閹凤拷

    濠电姷鏁告慨鐑姐€傛禒瀣劦妞ゆ巻鍋撻柛鐔锋健閸┾偓妞ゆ巻鍋撶紓宥咃躬楠炲啫螣鐠囪尙绐為梺褰掑亰閸撴盯鎮惧ú顏呪拺闂傚牊鍗曢崼銉ョ柧婵犲﹤瀚崣蹇旂節婵犲倻澧涢柛瀣ㄥ妽閵囧嫰寮介妸褋鈧帡鏌熼挊澶婃殻闁哄瞼鍠栭幃婊堝煛閸屾稓褰嬮柣搴ゎ潐濞叉ê鐣濈粙璺ㄦ殾闁割偅娲栭悡娑㈡煕鐏炲墽鐭嬫繛鍫熸倐濮婄粯鎷呯粵瀣異闂佹悶鍔嬮崡鍐茬暦閵忋倕鍐€妞ゆ劑鍎卞皬闂備焦瀵х粙鎴犫偓姘煎弮瀹曚即宕卞Ο闀愮盎闂侀潧鐗嗛幊搴㈡叏椤掆偓閳规垿鍩ラ崱妞剧凹濠电姰鍨洪敋閾荤偞淇婇妶鍛櫤闁稿鍊圭换娑㈠幢濡纰嶉柣搴㈣壘椤︾敻寮诲鍫闂佸憡鎸鹃崰搴敋閿濆鏁嗗〒姘功閻绻涢幘鏉戠劰闁稿鎹囬弻锝呪槈濞嗘劕纾抽梺鍝勬湰缁嬫垿鍩為幋锕€宸濇い鏇炴噺閳诲﹦绱撻崒娆戝妽妞ゃ劌鎳橀幆宀勫磼閻愰潧绁﹂柟鍏肩暘閸斿矂鎮為崹顐犱簻闁圭儤鍨甸鈺呮倵濮橆剦妲归柕鍥у瀵粙濡歌閸c儳绱撴担绛嬪殭婵☆偅绻堝濠氭偄绾拌鲸鏅i悷婊冪Ч閹﹢鎳犻鍌滐紲闁哄鐗勯崝搴g不閻愮儤鐓涢悘鐐跺Г閸犳﹢鏌℃担鐟板鐎规洜鍠栭、姗€鎮╅搹顐ら拻闂傚倷娴囧畷鍨叏閹惰姤鈷旂€广儱顦崹鍌炴煢濡尨绱氶柨婵嗩槸缁€瀣亜閺嶃劎鈽夋繛鍫熺矒濮婅櫣娑甸崨顔俱€愬銈庡亝濞茬喖宕洪埀顒併亜閹哄棗浜鹃梺鎸庢穿婵″洤危閹版澘绫嶉柛顐g箘椤撴椽姊虹紒妯哄鐎殿噮鍓欒灃闁告侗鍠氶崢鎼佹⒑閸撴彃浜介柛瀣閹﹢鏁冮崒娑氬幈闁诲函缍嗛崑鍡樻櫠椤掑倻纾奸柛灞剧☉缁椦囨煙閻熸澘顏柟鐓庢贡閹叉挳宕熼棃娑欐珡闂傚倸鍊风粈渚€骞栭銈傚亾濮樺崬鍘寸€规洖缍婇弻鍡楊吋閸涱垽绱遍柣搴$畭閸庨亶藝娴兼潙纾跨€广儱顦伴悡鏇㈡煛閸ャ儱濡煎褜鍨伴湁闁绘ǹ绉鍫熺畳闂備焦瀵х换鍌毼涘Δ鍛厺闁哄洢鍨洪悡鍐喐濠婂牆绀堟慨妯挎硾閽冪喖鏌曟繛褍瀚烽崑銊╂⒑缂佹ê濮囨い鏇ㄥ弮閸┿垽寮撮姀鈥斥偓鐢告煥濠靛棗鈧懓鈻嶉崶銊d簻闊洦绋愰幉楣冩煛鐏炵偓绀嬬€规洟浜堕、姗€鎮㈡總澶夌处

    重磅专题
    往期文章
    最新文章