Produced by Araxis Merge on 5/25/2018 9:21:43 AM Central Daylight Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
| # | Location | File | Last Modified |
|---|---|---|---|
| 1 | C:\AraxisMergeCompare\Pri_un\ZIP\DSM-cif\Direct Implementation\java\install\build\download\apache-tomcat-6.0.37.tar\apache-tomcat-6.0.37\webapps\docs | jndi-datasource-examples-howto.html | Mon Apr 29 09:36:10 2013 UTC |
| 2 | C:\AraxisMergeCompare\Pri_re\ZIP\DSM-cif\Direct Implementation\java\install\build\download\apache-tomcat-6.0.37.tar\apache-tomcat-6.0.37\webapps\docs | jndi-datasource-examples-howto.html | Thu May 24 19:50:45 2018 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 2 | 1316 |
| Changed | 1 | 2 |
| Inserted | 0 | 0 |
| Removed | 0 | 0 |
| Whitespace | |
|---|---|
| Character case | Differences in character case are significant |
| Line endings | Differences in line endings (CR and LF characters) are ignored |
| CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
| 1 | <html><hea d><META ht tp-equiv=" Content-Ty pe" conten t="text/ht ml; charse t=iso-8859 -1"><title >Apache To mcat 6.0 ( 6.0.37) - JNDI Datas ource HOW- TO</title> <meta name ="author" content="L es Hughes" ><meta nam e="author" content=" David Hara burda"><me ta name="a uthor" con tent="Glen n Nielsen" ><meta nam e="author" content=" Yoav Shapi ra"><style type="tex t/css" med ia="print" > | |
| 2 | .noPri nt {displa y: none;} | |
| 3 | td#mai nBody {wid th: 100%;} | |
| 4 | </st yle></head ><body bgc olor="#fff fff" text= "#000000" link="#525 D76" alink ="#525D76" vlink="#5 25D76"><ta ble border ="0" width ="100%" ce llspacing= "0"><!--PA GE HEADER- -><tr><td> <!--PROJEC T LOGO-->< a href="ht tp://tomca t.apache.o rg/"><img src="./ima ges/tomcat .gif" alig n="right" alt=" | |
| 5 | The Apache Tom cat Servle t/JSP Cont ainer | |
| 6 | " bord er="0"></a ></td><td> <h1><font face="aria l,helvetic a,sanserif ">Apache T omcat 6.0< /font></h1 ><font fac e="arial,h elvetica,s anserif">V ersion 6.0 .37, Apr 2 9 2013</fo nt></td><t d><!--APAC HE LOGO--> <a href="h ttp://www. apache.org /"><img sr c="./image s/asf-logo .gif" alig n="right" alt="Apach e Logo" bo rder="0">< /a></td></ tr></table ><table bo rder="0" w idth="100% " cellspac ing="4"><! --HEADER S EPARATOR-- ><tr><td c olspan="2" ><hr nosha de="noshad e" size="1 "></td></t r><tr><!-- LEFT SIDE NAVIGATION --><td wid th="20%" v align="top " nowrap=" nowrap" cl ass="noPri nt"><p><st rong>Links </strong>< /p><ul><li ><a href=" index.html ">Docs Hom e</a></li> <li><a hre f="http:// wiki.apach e.org/tomc at/FAQ">FA Q</a></li> </ul><p><s trong>User Guide</st rong></p>< ul><li><a href="intr oduction.h tml">1) In troduction </a></li>< li><a href ="setup.ht ml">2) Set up</a></li ><li><a hr ef="appdev /index.htm l">3) Firs t webapp</ a></li><li ><a href=" deployer-h owto.html" >4) Deploy er</a></li ><li><a hr ef="manage r-howto.ht ml">5) Man ager</a></ li><li><a href="real m-howto.ht ml">6) Rea lms and AA A</a></li> <li><a hre f="securit y-manager- howto.html ">7) Secur ity Manage r</a></li> <li><a hre f="jndi-re sources-ho wto.html"> 8) JNDI Re sources</a ></li><li> <a href="j ndi-dataso urce-examp les-howto. html">9) J DBC DataSo urces</a>< /li><li><a href="cla ss-loader- howto.html ">10) Clas sloading</ a></li><li ><a href=" jasper-how to.html">1 1) JSPs</a ></li><li> <a href="s sl-howto.h tml">12) S SL</a></li ><li><a hr ef="ssi-ho wto.html"> 13) SSI</a ></li><li> <a href="c gi-howto.h tml">14) C GI</a></li ><li><a hr ef="proxy- howto.html ">15) Prox y Support< /a></li><l i><a href= "mbeans-de scriptor-h owto.html" >16) MBean Descripto r</a></li> <li><a hre f="default -servlet.h tml">17) D efault Ser vlet</a></ li><li><a href="clus ter-howto. html">18) Clustering </a></li>< li><a href ="balancer -howto.htm l">19) Loa d Balancer </a></li>< li><a href ="connecto rs.html">2 0) Connect ors</a></l i><li><a h ref="monit oring.html ">21) Moni toring and Managemen t</a></li> <li><a hre f="logging .html">22) Logging</ a></li><li ><a href=" apr.html"> 23) APR/Na tive</a></ li><li><a href="virt ual-hostin g-howto.ht ml">24) Vi rtual Host ing</a></l i><li><a h ref="aio.h tml">25) A dvanced IO </a></li>< li><a href ="extras.h tml">26) A dditional Components </a></li>< li><a href ="maven-ja rs.html">2 7) Maveniz ed</a></li ></ul><p>< strong>Ref erence</st rong></p>< ul><li><a href="RELE ASE-NOTES. txt">Relea se Notes</ a></li><li ><a href=" config/ind ex.html">C onfigurati on</a></li ><li><a hr ef="api/in dex.html"> Javadocs</ a></li><li ><a href=" http://tom cat.apache .org/conne ctors-doc/ ">JK 1.2 D ocumentati on</a></li ></ul><p>< strong>Apa che Tomcat Developme nt</strong ></p><ul>< li><a href ="building .html">Bui lding</a>< /li><li><a href="cha ngelog.htm l">Changel og</a></li ><li><a hr ef="http:/ /wiki.apac he.org/tom cat/Tomcat Versions"> Status</a> </li><li>< a href="de velopers.h tml">Devel opers</a>< /li><li><a href="arc hitecture/ index.html ">Architec ture</a></ li><li><a href="func specs/inde x.html">Fu nctional S pecs.</a>< /li></ul>< /td><!--RI GHT SIDE M AIN BODY-- ><td width ="80%" val ign="top" align="lef t" id="mai nBody"><h1 >Apache To mcat 6.0</ h1><h2>JND I Datasour ce HOW-TO< /h2><table border="0 " cellspac ing="0" ce llpadding= "2"><tr><t d bgcolor= "#525D76"> <font colo r="#ffffff " face="ar ial,helvet ica.sanser if"><a nam e="Table o f Contents "><!--()-- ></a><a na me="Table_ of_Content s"><strong >Table of Contents</ strong></a ></font></ td></tr><t r><td><blo ckquote> | |
| 7 | <ul><li><a href="#In troduction ">Introduc tion</a></ li><li><a href="#Dri verManager ,_the_serv ice_provid er_mechani sm_and_mem ory_leaks" >DriverMan ager, the service pr ovider mec hanism and memory le aks</a></l i><li><a h ref="#Data base_Conne ction_Pool _(DBCP)_Co nfiguratio ns">Databa se Connect ion Pool ( DBCP) Conf igurations </a><ol><l i><a href= "#Installa tion">Inst allation</ a></li><li ><a href=" #Preventin g_database _connectio n_pool_lea ks">Preven ting datab ase connec tion pool leaks</a>< /li><li><a href="#My SQL_DBCP_E xample">My SQL DBCP E xample</a> </li><li>< a href="#O racle_8i,_ 9i_&_10g"> Oracle 8i, 9i & 10g</a></l i><li><a h ref="#Post greSQL">Po stgreSQL</ a></li></o l></li><li ><a href=" #Non-DBCP_ Solutions" >Non-DBCP Solutions< /a></li><l i><a href= "#Oracle_8 i_with_OCI _client">O racle 8i w ith OCI cl ient</a><o l><li><a h ref="#Orac le_8i_with _OCI_clien t/Introduc tion">Intr oduction</ a></li><li ><a href=" #Putting_i t_all_toge ther">Putt ing it all together< /a></li></ ol></li><l i><a href= "#Common_P roblems">C ommon Prob lems</a><o l><li><a h ref="#Inte rmittent_D atabase_Co nnection_F ailures">I ntermitten t Database Connectio n Failures </a></li>< li><a href ="#Random_ Connection _Closed_Ex ceptions"> Random Con nection Cl osed Excep tions</a>< /li><li><a href="#Co ntext_vers us_GlobalN amingResou rces">Cont ext versus GlobalNam ingResourc es</a></li ><li><a hr ef="#JNDI_ Resource_N aming_and_ Realm_Inte raction">J NDI Resour ce Naming and Realm Interactio n</a></li> </ol></li> </ul> | |
| 8 | </blockquo te></td></ tr></table ><table bo rder="0" c ellspacing ="0" cellp adding="2" ><tr><td b gcolor="#5 25D76"><fo nt color=" #ffffff" f ace="arial ,helvetica .sanserif" ><a name=" Introducti on"><stron g>Introduc tion</stro ng></a></f ont></td>< /tr><tr><t d><blockqu ote> | |
| 9 | ||
| 10 | <p>JNDI Da tasource c onfigurati on is cove red extens ively in t he | |
| 11 | JNDI-Resou rces-HOWTO . However , feedback from <cod e>tomcat-u ser</code> has | |
| 12 | shown that specifics for indiv idual conf igurations can be ra ther trick y.</p> | |
| 13 | ||
| 14 | <p>Here th en are som e example configurat ions that have been posted to | |
| 15 | tomcat-use r for popu lar databa ses and so me general tips for db usage.< /p> | |
| 16 | ||
| 17 | <p>You sho uld be awa re that si nce these notes are derived fr om configu ration | |
| 18 | and/or fee dback post ed to <cod e>tomcat-u ser</code> YMMV :-). Please le t us | |
| 19 | know if yo u have any other tes ted config urations t hat you fe el may be of use | |
| 20 | to the wid er audienc e, or if y ou feel we can impro ve this se ction in a nyway.</p> | |
| 21 | ||
| 22 | <p> | |
| 23 | <b>Please note that JNDI resou rce config uration ch anged some what betwe en | |
| 24 | Tomcat 5.0 .x and Tom cat 5.5.x. </b> You will most likely nee d to modif y older | |
| 25 | JNDI resou rce config urations t o match th e syntax i n the exam ple below in order | |
| 26 | to make th em work in Tomcat 6. x.x. | |
| 27 | </p> | |
| 28 | ||
| 29 | <p> | |
| 30 | Also, plea se note th at JNDI Da taSource c onfigurati on in gene ral, and t his | |
| 31 | tutorial i n particul ar, assume s that you have read and under stood the | |
| 32 | <a href="c onfig/cont ext.html"> Context</a > and | |
| 33 | <a href="c onfig/host .html">Hos t</a> conf iguration references , includin g | |
| 34 | the sectio n about Au tomatic Ap plication Deployment in the la tter refer ence. | |
| 35 | </p> | |
| 36 | </blockquo te></td></ tr></table ><table bo rder="0" c ellspacing ="0" cellp adding="2" ><tr><td b gcolor="#5 25D76"><fo nt color=" #ffffff" f ace="arial ,helvetica .sanserif" ><a name=" DriverMana ger, the s ervice pro vider mech anism and memory lea ks"><!--() --></a><a name="Driv erManager, _the_servi ce_provide r_mechanis m_and_memo ry_leaks"> <strong>Dr iverManage r, the ser vice provi der mechan ism and me mory leaks </strong>< /a></font> </td></tr> <tr><td><b lockquote> | |
| 37 | ||
| 38 | <p><code>j ava.sql.Dr iverManage r</code> s upports th e | |
| 39 | <a href="h ttp://docs .oracle.co m/javase/6 /docs/api/ index.html ?java/sql/ DriverMana ger.html"> service | |
| 40 | provider</ a> mechani sm. This f eature is that all t he availab le JDBC dr ivers | |
| 41 | that annou nce themse lves by pr oviding a <code>META -INF/servi ces/java.s ql.Driver< /code> | |
| 42 | file are a utomatical ly discove red, loade d and regi stered, | |
| 43 | relieving you from t he need to load the database d river expl icitly bef ore | |
| 44 | you create a JDBC co nnection. | |
| 45 | However, t he impleme ntation is fundament ally broke n in all J ava versio ns for | |
| 46 | a servlet container environmen t. The pro blem is th at | |
| 47 | <code>java .sql.Drive rManager</ code> will scan for the driver s only onc e.</p> | |
| 48 | ||
| 49 | <p>The <a href="conf ig/listene rs.html">J RE Memory Leak Preve ntion List ener</a> | |
| 50 | that is in cluded wit h Apache T omcat solv es this by triggerin g the driv ers scan | |
| 51 | during Tom cat startu p. This is enabled b y default. It means that only | |
| 52 | libraries visible to the liste ner such a s the ones in | |
| 53 | <code>$CAT ALINA_BASE /lib</code > will be scanned fo r database drivers. | |
| 54 | If you are consideri ng disabli ng this fe ature, not e that | |
| 55 | the scan w ould be tr iggered by the first web appli cation tha t is | |
| 56 | using JDBC , leading to failure s when thi s web appl ication is reloaded | |
| 57 | and for ot her web ap plications that rely on this f eature. | |
| 58 | </p> | |
| 59 | ||
| 60 | <p>Thus, t he web app lications that have database d rivers in their | |
| 61 | <code>WEB- INF/lib</c ode> direc tory canno t rely on the servic e provider | |
| 62 | mechanism and should register the driver s explicit ly.</p> | |
| 63 | ||
| 64 | <p>The lis t of drive rs in <cod e>java.sql .DriverMan ager</code > is also | |
| 65 | a known so urce of me mory leaks . Any Driv ers regist ered | |
| 66 | by a web a pplication must be d eregistere d when the web appli cation sto ps. | |
| 67 | Tomcat wil l attempt to automat ically dis cover and deregister any | |
| 68 | JDBC drive rs loaded by the web applicati on class l oader when the web | |
| 69 | applicatio n stops. | |
| 70 | However, i t is expec ted that a pplication s do this for themse lves via | |
| 71 | a <code>Se rvletConte xtListener </code>. | |
| 72 | </p> | |
| 73 | ||
| 74 | </blockquo te></td></ tr></table ><table bo rder="0" c ellspacing ="0" cellp adding="2" ><tr><td b gcolor="#5 25D76"><fo nt color=" #ffffff" f ace="arial ,helvetica .sanserif" ><a name=" Database C onnection Pool (DBCP ) Configur ations"><! --()--></a ><a name=" Database_C onnection_ Pool_(DBCP )_Configur ations"><s trong>Data base Conne ction Pool (DBCP) Co nfiguratio ns</strong ></a></fon t></td></t r><tr><td> <blockquot e> | |
| 75 | ||
| 76 | <p>The def ault datab ase connec tion pool implementa tion in Ap ache Tomca t | |
| 77 | relies on the librar ies from t he | |
| 78 | <a href="h ttp://comm ons.apache .org/">Apa che Common s</a> proj ect. | |
| 79 | The follow ing librar ies are us ed: | |
| 80 | </p> | |
| 81 | ||
| 82 | <ul> | |
| 83 | <li>Common s DBCP</li > | |
| 84 | <li>Common s Pool</li > | |
| 85 | </ul> | |
| 86 | ||
| 87 | <p> | |
| 88 | These libr aries are located in a single JAR at | |
| 89 | <code>$CAT ALINA_HOME /lib/tomca t-dbcp.jar </code>. H owever, | |
| 90 | only the c lasses nee ded for co nnection p ooling hav e been inc luded, and the | |
| 91 | packages h ave been r enamed to avoid inte rfering wi th applica tions. | |
| 92 | </p> | |
| 93 | ||
| 94 | <p>DBCP 1. 3 provides support f or JDBC 3. 0.</p> | |
| 95 | ||
| 96 | <table bor der="0" ce llspacing= "0" cellpa dding="2"> <tr><td bg color="#82 8DA6"><fon t color="# ffffff" fa ce="arial, helvetica. sanserif"> <a name="I nstallatio n"><strong >Installat ion</stron g></a></fo nt></td></ tr><tr><td ><blockquo te> | |
| 97 | ||
| 98 | <p>See the <a href=" http://com mons.apach e.org/dbcp /configura tion.html" > | |
| 99 | DBCP docum entation</ a> for a c omplete li st of conf iguration parameters . | |
| 100 | </p> | |
| 101 | ||
| 102 | </blockquo te></td></ tr></table > | |
| 103 | ||
| 104 | <table bor der="0" ce llspacing= "0" cellpa dding="2"> <tr><td bg color="#82 8DA6"><fon t color="# ffffff" fa ce="arial, helvetica. sanserif"> <a name="P reventing database c onnection pool leaks "><!--()-- ></a><a na me="Preven ting_datab ase_connec tion_pool_ leaks"><st rong>Preve nting data base conne ction pool leaks</st rong></a>< /font></td ></tr><tr> <td><block quote> | |
| 105 | ||
| 106 | <p> | |
| 107 | A database connectio n pool cre ates and m anages a p ool of con nections | |
| 108 | to a datab ase. Recyc ling and r eusing alr eady exist ing connec tions | |
| 109 | to a datab ase is mor e efficien t than ope ning a new connectio n. | |
| 110 | </p> | |
| 111 | ||
| 112 | <p> | |
| 113 | There is o ne problem with conn ection poo ling. A w eb applica tion has | |
| 114 | to explici tly close ResultSet' s, Stateme nt's, and Connection 's. | |
| 115 | Failure of a web app lication t o close th ese resour ces can re sult in | |
| 116 | them never being ava ilable aga in for reu se, a data base conne ction pool "leak". | |
| 117 | This can e ventually result in your web a pplication database connection s failing | |
| 118 | if there a re no more available connectio ns.</p> | |
| 119 | ||
| 120 | <p> | |
| 121 | There is a solution to this pr oblem. Th e Apache C ommons DBC P can be | |
| 122 | configured to track and recove r these ab andoned da tabase con nections. Not | |
| 123 | only can i t recover them, but also gener ate a stac k trace fo r the code | |
| 124 | which open ed these r esources a nd never c losed them .</p> | |
| 125 | ||
| 126 | <p> | |
| 127 | To configu re a DBCP DataSource so that a bandoned d atabase co nnections are | |
| 128 | removed an d recycled add the f ollowing a ttribute t o the | |
| 129 | <code>Reso urce</code > configur ation for your DBCP DataSource : | |
| 130 | </p> | |
| 131 | ||
| 132 | <div align ="left"><t able cells pacing="4" cellpaddi ng="0" bor der="0"><t r><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td><t d bgcolor= "#023264" width="1" height="1" ><img src= "./images/ void.gif" alt="" wid th="1" hei ght="1" vs pace="0" h space="0" border="0" ></td></tr ><tr><td b gcolor="#0 23264" wid th="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#ffff ff" height ="1"><pre> removeAban doned="tru e"</pre></ td><td bgc olor="#023 264" width ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> </tr><tr>< td bgcolor ="#023264" width="1" height="1 "><img src ="./images /void.gif" alt="" wi dth="1" he ight="1" v space="0" hspace="0" border="0 "></td><td bgcolor=" #023264" h eight="1"> <img src=" ./images/v oid.gif" a lt="" widt h="1" heig ht="1" vsp ace="0" hs pace="0" b order="0"> </td><td b gcolor="#0 23264" wid th="1" hei ght="1"><i mg src="./ images/voi d.gif" alt ="" width= "1" height ="1" vspac e="0" hspa ce="0" bor der="0"></ td></tr></ table></di v> | |
| 133 | ||
| 134 | <p> | |
| 135 | When avail able datab ase connec tions run low DBCP w ill recove r and recy cle | |
| 136 | any abando ned databa se connect ions it fi nds. The d efault is <code>fals e</code>. | |
| 137 | </p> | |
| 138 | ||
| 139 | <p> | |
| 140 | Use the <c ode>remove AbandonedT imeout</co de> attrib ute to set the numbe r | |
| 141 | of seconds a databas e connecti on has bee n idle bef ore it is considered abandoned . | |
| 142 | </p> | |
| 143 | ||
| 144 | <div align ="left"><t able cells pacing="4" cellpaddi ng="0" bor der="0"><t r><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td><t d bgcolor= "#023264" width="1" height="1" ><img src= "./images/ void.gif" alt="" wid th="1" hei ght="1" vs pace="0" h space="0" border="0" ></td></tr ><tr><td b gcolor="#0 23264" wid th="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#ffff ff" height ="1"><pre> removeAban donedTimeo ut="60"</p re></td><t d bgcolor= "#023264" width="1"> <img src=" ./images/v oid.gif" a lt="" widt h="1" heig ht="1" vsp ace="0" hs pace="0" b order="0"> </td></tr> <tr><td bg color="#02 3264" widt h="1" heig ht="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#0232 64" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " width="1 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td></ tr></table ></div> | |
| 145 | ||
| 146 | <p> | |
| 147 | The defaul t timeout for removi ng abandon ed connect ions is 30 0 seconds. | |
| 148 | </p> | |
| 149 | ||
| 150 | <p> | |
| 151 | The <code> logAbandon ed</code> attribute can be set to <code> true</code > | |
| 152 | if you wan t DBCP to log a stac k trace of the code which aban doned the | |
| 153 | database c onnection resources. | |
| 154 | </p> | |
| 155 | <div align ="left"><t able cells pacing="4" cellpaddi ng="0" bor der="0"><t r><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td><t d bgcolor= "#023264" width="1" height="1" ><img src= "./images/ void.gif" alt="" wid th="1" hei ght="1" vs pace="0" h space="0" border="0" ></td></tr ><tr><td b gcolor="#0 23264" wid th="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#ffff ff" height ="1"><pre> logAbandon ed="true"< /pre></td> <td bgcolo r="#023264 " width="1 "><img src ="./images /void.gif" alt="" wi dth="1" he ight="1" v space="0" hspace="0" border="0 "></td></t r><tr><td bgcolor="# 023264" wi dth="1" he ight="1">< img src=". /images/vo id.gif" al t="" width ="1" heigh t="1" vspa ce="0" hsp ace="0" bo rder="0">< /td><td bg color="#02 3264" heig ht="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> </tr></tab le></div> | |
| 156 | <p> | |
| 157 | The defaul t is <code >false</co de>. | |
| 158 | </p> | |
| 159 | ||
| 160 | </blockquo te></td></ tr></table > | |
| 161 | ||
| 162 | <table bor der="0" ce llspacing= "0" cellpa dding="2"> <tr><td bg color="#82 8DA6"><fon t color="# ffffff" fa ce="arial, helvetica. sanserif"> <a name="M ySQL DBCP Example">< !--()--></ a><a name= "MySQL_DBC P_Example" ><strong>M ySQL DBCP Example</s trong></a> </font></t d></tr><tr ><td><bloc kquote> | |
| 163 | ||
| 164 | <h3>0. Int roduction< /h3> | |
| 165 | <p>Version s of <a hr ef="http:/ /www.mysql .com/produ cts/mysql/ index.html ">MySQL</a > and JDBC | |
| 166 | drivers th at have be en reporte d to work: | |
| 167 | </p> | |
| 168 | <ul> | |
| 169 | <li>MySQL 3.23.47, M ySQL 3.23. 47 using I nnoDB,, My SQL 3.23.5 8, MySQL 4.0.1alpha </li> | |
| 170 | <li><a hre f="http:// www.mysql. com/produc ts/connect or-j">Conn ector/J</a > 3.0.11-s table (the official JDBC Drive r)</li> | |
| 171 | <li><a hre f="http:// mmmysql.so urceforge. net">mm.my sql</a> 2. 0.14 (an o ld 3rd par ty JDBC Dr iver)</li> | |
| 172 | </ul> | |
| 173 | ||
| 174 | <p>Before you procee d, don't f orget to c opy the JD BC Driver' s jar into <code>$CA TALINA_HOM E/lib</cod e>.</p> | |
| 175 | ||
| 176 | <h3>1. MyS QL configu ration</h3 > | |
| 177 | <p> | |
| 178 | Ensure tha t you foll ow these i nstruction s as varia tions can cause prob lems. | |
| 179 | </p> | |
| 180 | ||
| 181 | <p>Create a new test user, a n ew databas e and a si ngle test table. | |
| 182 | Your MySQL user <str ong>must</ strong> ha ve a passw ord assign ed. The dr iver | |
| 183 | will fail if you try to connec t with an empty pass word. | |
| 184 | </p> | |
| 185 | <div align ="left"><t able cells pacing="4" cellpaddi ng="0" bor der="0"><t r><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td><t d bgcolor= "#023264" width="1" height="1" ><img src= "./images/ void.gif" alt="" wid th="1" hei ght="1" vs pace="0" h space="0" border="0" ></td></tr ><tr><td b gcolor="#0 23264" wid th="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#ffff ff" height ="1"><pre> | |
| 186 | mysql> GRANT ALL PRIVILEGES ON *.* TO javauser@ localhost | |
| 187 | -> IDENTIFI ED BY 'jav adude' WIT H GRANT OP TION; | |
| 188 | mysql> create dat abase java test; | |
| 189 | mysql> use javate st; | |
| 190 | mysql> create tab le testdat a ( | |
| 191 | -> id int n ot null au to_increme nt primary key, | |
| 192 | -> foo varc har(25), | |
| 193 | -> bar int) ; | |
| 194 | </pre></td ><td bgcol or="#02326 4" width=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td></ tr><tr><td bgcolor=" #023264" w idth="1" h eight="1"> <img src=" ./images/v oid.gif" a lt="" widt h="1" heig ht="1" vsp ace="0" hs pace="0" b order="0"> </td><td b gcolor="#0 23264" hei ght="1"><i mg src="./ images/voi d.gif" alt ="" width= "1" height ="1" vspac e="0" hspa ce="0" bor der="0"></ td><td bgc olor="#023 264" width ="1" heigh t="1"><img src="./im ages/void. gif" alt=" " width="1 " height=" 1" vspace= "0" hspace ="0" borde r="0"></td ></tr></ta ble></div> | |
| 195 | <blockquot e> | |
| 196 | <strong>No te:</stron g> the abo ve user sh ould be re moved once testing i s | |
| 197 | complete! | |
| 198 | </blockquo te> | |
| 199 | ||
| 200 | <p>Next in sert some test data into the t estdata ta ble. | |
| 201 | </p> | |
| 202 | <div align ="left"><t able cells pacing="4" cellpaddi ng="0" bor der="0"><t r><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td><t d bgcolor= "#023264" width="1" height="1" ><img src= "./images/ void.gif" alt="" wid th="1" hei ght="1" vs pace="0" h space="0" border="0" ></td></tr ><tr><td b gcolor="#0 23264" wid th="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#ffff ff" height ="1"><pre> | |
| 203 | mysql> insert int o testdata values(nu ll, 'hello ', 12345); | |
| 204 | Query OK, 1 row affe cted (0.00 sec) | |
| 205 | ||
| 206 | mysql> select * f rom testda ta; | |
| 207 | +----+---- ---+------ -+ | |
| 208 | | ID | FOO | BAR | | |
| 209 | +----+---- ---+------ -+ | |
| 210 | | 1 | hel lo | 12345 | | |
| 211 | +----+---- ---+------ -+ | |
| 212 | 1 row in s et (0.00 s ec) | |
| 213 | ||
| 214 | mysql> | |
| 215 | </pre></td ><td bgcol or="#02326 4" width=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td></ tr><tr><td bgcolor=" #023264" w idth="1" h eight="1"> <img src=" ./images/v oid.gif" a lt="" widt h="1" heig ht="1" vsp ace="0" hs pace="0" b order="0"> </td><td b gcolor="#0 23264" hei ght="1"><i mg src="./ images/voi d.gif" alt ="" width= "1" height ="1" vspac e="0" hspa ce="0" bor der="0"></ td><td bgc olor="#023 264" width ="1" heigh t="1"><img src="./im ages/void. gif" alt=" " width="1 " height=" 1" vspace= "0" hspace ="0" borde r="0"></td ></tr></ta ble></div> | |
| 216 | ||
| 217 | <h3>2. Con text confi guration</ h3> | |
| 218 | <p>Configu re the JND I DataSour ce in Tomc at by addi ng a decla ration for your | |
| 219 | resource t o your <a href="conf ig/context .html">Con text</a>.< /p> | |
| 220 | <p>For exa mple:</p> | |
| 221 | <div align ="left"><t able cells pacing="4" cellpaddi ng="0" bor der="0"><t r><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td><t d bgcolor= "#023264" width="1" height="1" ><img src= "./images/ void.gif" alt="" wid th="1" hei ght="1" vs pace="0" h space="0" border="0" ></td></tr ><tr><td b gcolor="#0 23264" wid th="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#ffff ff" height ="1"><pre> | |
| 222 | <Contex t> | |
| 223 | ||
| 224 | <!- - maxActiv e: Maximum number of database connection s in pool. Make sure you | |
| 225 | c onfigure y our mysqld max_conne ctions lar ge enough to handle | |
| 226 | a ll of your db connec tions. Set to -1 for no limit. | |
| 227 | - -> | |
| 228 | ||
| 229 | <!- - maxIdle: Maximum n umber of i dle databa se connect ions to re tain in po ol. | |
| 230 | S et to -1 f or no limi t. See al so the DBC P document ation on t his | |
| 231 | a nd the min EvictableI dleTimeMil lis config uration pa rameter. | |
| 232 | - -> | |
| 233 | ||
| 234 | <!- - maxWait: Maximum t ime to wai t for a da tabase con nection to become av ailable | |
| 235 | i n ms, in t his exampl e 10 secon ds. An Exc eption is thrown if | |
| 236 | t his timeou t is excee ded. Set to -1 to w ait indefi nitely. | |
| 237 | - -> | |
| 238 | ||
| 239 | <!- - username and passw ord: MySQL username and passwo rd for dat abase conn ections - -> | |
| 240 | ||
| 241 | <!- - driverCl assName: C lass name for the ol d mm.mysql JDBC driv er is | |
| 242 | o rg.gjt.mm. mysql.Driv er - we re commend us ing Connec tor/J thou gh. | |
| 243 | C lass name for the of ficial MyS QL Connect or/J drive r is com.m ysql.jdbc. Driver. | |
| 244 | - -> | |
| 245 | ||
| 246 | <!- - url: The JDBC conn ection url for conne cting to y our MySQL database. | |
| 247 | - -> | |
| 248 | ||
| 249 | <Reso urce name= "jdbc/Test DB" auth=" Container" type="jav ax.sql.Dat aSource" | |
| 250 | maxAc tive="100" maxIdle=" 30" maxWai t="10000" | |
| 251 | usern ame="javau ser" passw ord="javad ude" drive rClassName ="com.mysq l.jdbc.Dri ver" | |
| 252 | url=" jdbc:mysql ://localho st:3306/ja vatest"/&g t; | |
| 253 | ||
| 254 | </Conte xt> | |
| 255 | </pre></td ><td bgcol or="#02326 4" width=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td></ tr><tr><td bgcolor=" #023264" w idth="1" h eight="1"> <img src=" ./images/v oid.gif" a lt="" widt h="1" heig ht="1" vsp ace="0" hs pace="0" b order="0"> </td><td b gcolor="#0 23264" hei ght="1"><i mg src="./ images/voi d.gif" alt ="" width= "1" height ="1" vspac e="0" hspa ce="0" bor der="0"></ td><td bgc olor="#023 264" width ="1" heigh t="1"><img src="./im ages/void. gif" alt=" " width="1 " height=" 1" vspace= "0" hspace ="0" borde r="0"></td ></tr></ta ble></div> | |
| 256 | ||
| 257 | <h3>3. web .xml confi guration</ h3> | |
| 258 | ||
| 259 | <p>Now cre ate a <cod e>WEB-INF/ web.xml</c ode> for t his test a pplication .</p> | |
| 260 | <div align ="left"><t able cells pacing="4" cellpaddi ng="0" bor der="0"><t r><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td><t d bgcolor= "#023264" width="1" height="1" ><img src= "./images/ void.gif" alt="" wid th="1" hei ght="1" vs pace="0" h space="0" border="0" ></td></tr ><tr><td b gcolor="#0 23264" wid th="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#ffff ff" height ="1"><pre> | |
| 261 | <web-ap p xmlns="h ttp://java .sun.com/x ml/ns/j2ee " | |
| 262 | xmlns: xsi="http: //www.w3.o rg/2001/XM LSchema-in stance" | |
| 263 | xsi:sc hemaLocati on="http:/ /java.sun. com/xml/ns /j2ee | |
| 264 | http://jav a.sun.com/ xml/ns/j2e e/web-app_ 2_4.xsd" | |
| 265 | versio n="2.4"> ; | |
| 266 | <desc ription> ;MySQL Tes t App</ descriptio n> | |
| 267 | <reso urce-ref&g t; | |
| 268 | < descriptio n>DB Co nnection&l t;/descrip tion> | |
| 269 | < res-ref-na me>jdbc /TestDB< ;/res-ref- name> | |
| 270 | < res-type&g t;javax.sq l.DataSour ce</res -type> | |
| 271 | < res-auth&g t;Containe r</res- auth> | |
| 272 | </res ource-ref& gt; | |
| 273 | </web-a pp> | |
| 274 | </pre></td ><td bgcol or="#02326 4" width=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td></ tr><tr><td bgcolor=" #023264" w idth="1" h eight="1"> <img src=" ./images/v oid.gif" a lt="" widt h="1" heig ht="1" vsp ace="0" hs pace="0" b order="0"> </td><td b gcolor="#0 23264" hei ght="1"><i mg src="./ images/voi d.gif" alt ="" width= "1" height ="1" vspac e="0" hspa ce="0" bor der="0"></ td><td bgc olor="#023 264" width ="1" heigh t="1"><img src="./im ages/void. gif" alt=" " width="1 " height=" 1" vspace= "0" hspace ="0" borde r="0"></td ></tr></ta ble></div> | |
| 275 | ||
| 276 | <h3>4. Tes t code</h3 > | |
| 277 | <p>Now cre ate a simp le <code>t est.jsp</c ode> page for use la ter. | |
| 278 | <div align ="left"><t able cells pacing="4" cellpaddi ng="0" bor der="0"><t r><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td><t d bgcolor= "#023264" width="1" height="1" ><img src= "./images/ void.gif" alt="" wid th="1" hei ght="1" vs pace="0" h space="0" border="0" ></td></tr ><tr><td b gcolor="#0 23264" wid th="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#ffff ff" height ="1"><pre> | |
| 279 | <%@ tag lib uri="h ttp://java .sun.com/j sp/jstl/sq l" prefix= "sql" %> ; | |
| 280 | <%@ tag lib uri="h ttp://java .sun.com/j sp/jstl/co re" prefix ="c" %> | |
| 281 | ||
| 282 | <sql:qu ery var="r s" dataSou rce="jdbc/ TestDB"> ; | |
| 283 | select id, foo, bar from testd ata | |
| 284 | </sql:q uery> | |
| 285 | ||
| 286 | <html&g t; | |
| 287 | <head > | |
| 288 | <ti tle>DB Test</t itle> | |
| 289 | </hea d> | |
| 290 | <body > | |
| 291 | ||
| 292 | <h2&g t;Results& lt;/h2> | |
| 293 | ||
| 294 | <c:forE ach var="r ow" items= "${rs.rows }"> | |
| 295 | Foo ${ row.foo}&l t;br/> | |
| 296 | Bar ${ row.bar}&l t;br/> | |
| 297 | </c:for Each> | |
| 298 | ||
| 299 | </bod y> | |
| 300 | </html& gt; | |
| 301 | </pre></td ><td bgcol or="#02326 4" width=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td></ tr><tr><td bgcolor=" #023264" w idth="1" h eight="1"> <img src=" ./images/v oid.gif" a lt="" widt h="1" heig ht="1" vsp ace="0" hs pace="0" b order="0"> </td><td b gcolor="#0 23264" hei ght="1"><i mg src="./ images/voi d.gif" alt ="" width= "1" height ="1" vspac e="0" hspa ce="0" bor der="0"></ td><td bgc olor="#023 264" width ="1" heigh t="1"><img src="./im ages/void. gif" alt=" " width="1 " height=" 1" vspace= "0" hspace ="0" borde r="0"></td ></tr></ta ble></div> | |
| 302 | </p> | |
| 303 | ||
| 304 | <p>That JS P page mak es use of <a href="h ttp://java .sun.com/p roducts/js p/jstl">JS TL</a>'s | |
| 305 | SQL and Co re taglibs . You can get it fro m | |
| 306 | <a href="h ttp://tomc at.apache. org/taglib s/standard /">Apache Tomcat Tag libs - Sta ndard Tag Library</a > | |
| 307 | project &m dash; just make sure you get a 1.1.x rel ease. Once you have JSTL, | |
| 308 | copy <code >jstl.jar< /code> and <code>sta ndard.jar< /code> to your web a pp's | |
| 309 | <code>WEB- INF/lib</c ode> direc tory. | |
| 310 | ||
| 311 | </p> | |
| 312 | ||
| 313 | <p>Finally deploy yo ur web app into <cod e>$CATALIN A_BASE/web apps</code > either | |
| 314 | as a warfi le called <code>DBTe st.war</co de> or int o a sub-di rectory ca lled | |
| 315 | <code>DBTe st</code>< /p> | |
| 316 | <p>Once de ployed, po int a brow ser at | |
| 317 | <code>http ://localho st:8080/DB Test/test. jsp</code> to view t he fruits of | |
| 318 | your hard work.</p> | |
| 319 | ||
| 320 | </blockquo te></td></ tr></table > | |
| 321 | ||
| 322 | <table bor der="0" ce llspacing= "0" cellpa dding="2"> <tr><td bg color="#82 8DA6"><fon t color="# ffffff" fa ce="arial, helvetica. sanserif"> <a name="O racle 8i, 9i & 10g"> <!--()-->< /a><a name ="Oracle_8 i,_9i_&_10 g"><strong >Oracle 8i , 9i & 10g</stro ng></a></f ont></td>< /tr><tr><t d><blockqu ote> | |
| 323 | <h3>0. Introducti on</h3> | |
| 324 | ||
| 325 | <p>Oracle requires m inimal cha nges from the MySQL configurat ion except for the | |
| 326 | usual gotc has :-)</p > | |
| 327 | <p>Drivers for older Oracle ve rsions may be distri buted as * .zip files rather | |
| 328 | than *.jar files. To mcat will only use < code>*.jar </code> fi les instal led in | |
| 329 | <code>$CAT ALINA_HOME /lib</code >. Therefo re <code>c lasses111. zip</code> | |
| 330 | or <code>c lasses12.z ip</code> will need to be rena med with a <code>.ja r</code> | |
| 331 | extension. Since jar files are zipfiles, there is n o need to unzip and jar these | |
| 332 | files - a simple ren ame will s uffice.</p > | |
| 333 | ||
| 334 | <p>For Ora cle 9i onw ards you s hould use <code>orac le.jdbc.Or acleDriver </code> | |
| 335 | rather tha n <code>or acle.jdbc. driver.Ora cleDriver< /code> as Oracle hav e stated | |
| 336 | that <code >oracle.jd bc.driver. OracleDriv er</code> is depreca ted and su pport | |
| 337 | for this d river clas s will be discontinu ed in the next major release. | |
| 338 | </p> | |
| 339 | ||
| 340 | <h3>1. Con text confi guration</ h3> | |
| 341 | <p>In a si milar mann er to the mysql conf ig above, you will n eed to def ine your | |
| 342 | Datasource in your < a href="co nfig/conte xt.html">C ontext</a> . Here we define a | |
| 343 | Datasource called my oracle usi ng the thi n driver t o connect as user sc ott, | |
| 344 | password t iger to th e sid call ed mysid. (Note: wit h the thin driver th is sid is | |
| 345 | not the sa me as the tnsname). The schema used will be the de fault sche ma for the | |
| 346 | user scott .</p> | |
| 347 | ||
| 348 | <p>Use of the OCI dr iver shoul d simply i nvolve a c hanging th in to oci in the URL string. | |
| 349 | </p> | |
| 350 | <div align ="left"><t able cells pacing="4" cellpaddi ng="0" bor der="0"><t r><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td><t d bgcolor= "#023264" width="1" height="1" ><img src= "./images/ void.gif" alt="" wid th="1" hei ght="1" vs pace="0" h space="0" border="0" ></td></tr ><tr><td b gcolor="#0 23264" wid th="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#ffff ff" height ="1"><pre> | |
| 351 | <Resour ce name="j dbc/myorac le" auth=" Container" | |
| 352 | type=" javax.sql. DataSource " driverCl assName="o racle.jdbc .OracleDri ver" | |
| 353 | url="j dbc:oracle :thin:@127 .0.0.1:152 1:mysid" | |
| 354 | userna me="scott" password= "tiger" ma xActive="2 0" maxIdle ="10" | |
| 355 | maxWai t="-1"/> ; | |
| 356 | </pre></td ><td bgcol or="#02326 4" width=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td></ tr><tr><td bgcolor=" #023264" w idth="1" h eight="1"> <img src=" ./images/v oid.gif" a lt="" widt h="1" heig ht="1" vsp ace="0" hs pace="0" b order="0"> </td><td b gcolor="#0 23264" hei ght="1"><i mg src="./ images/voi d.gif" alt ="" width= "1" height ="1" vspac e="0" hspa ce="0" bor der="0"></ td><td bgc olor="#023 264" width ="1" heigh t="1"><img src="./im ages/void. gif" alt=" " width="1 " height=" 1" vspace= "0" hspace ="0" borde r="0"></td ></tr></ta ble></div> | |
| 357 | ||
| 358 | <h3>2. web.xml co nfiguratio n</h3> | |
| 359 | <p>You sho uld ensure that you respect th e element ordering d efined by the DTD wh en you | |
| 360 | create you applicati ons web.xm l file.</p > | |
| 361 | <div align ="left"><t able cells pacing="4" cellpaddi ng="0" bor der="0"><t r><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td><t d bgcolor= "#023264" width="1" height="1" ><img src= "./images/ void.gif" alt="" wid th="1" hei ght="1" vs pace="0" h space="0" border="0" ></td></tr ><tr><td b gcolor="#0 23264" wid th="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#ffff ff" height ="1"><pre> | |
| 362 | <resour ce-ref> | |
| 363 | <descr iption> Oracle Dat asource ex ample</ descriptio n> | |
| 364 | <res-r ef-name> ;jdbc/myor acle</r es-ref-nam e> | |
| 365 | <res-t ype>jav ax.sql.Dat aSource< ;/res-type > | |
| 366 | <res-a uth>Con tainer< /res-auth& gt; | |
| 367 | </resou rce-ref> ; | |
| 368 | </pre></td ><td bgcol or="#02326 4" width=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td></ tr><tr><td bgcolor=" #023264" w idth="1" h eight="1"> <img src=" ./images/v oid.gif" a lt="" widt h="1" heig ht="1" vsp ace="0" hs pace="0" b order="0"> </td><td b gcolor="#0 23264" hei ght="1"><i mg src="./ images/voi d.gif" alt ="" width= "1" height ="1" vspac e="0" hspa ce="0" bor der="0"></ td><td bgc olor="#023 264" width ="1" heigh t="1"><img src="./im ages/void. gif" alt=" " width="1 " height=" 1" vspace= "0" hspace ="0" borde r="0"></td ></tr></ta ble></div> | |
| 369 | <h3>3. C ode exampl e</h3> | |
| 370 | <p>You can use the s ame exampl e applicat ion as abo ve (asumin g you crea te the req uired DB | |
| 371 | instance, tables etc .) replaci ng the Dat asource co de with so mething li ke</p> | |
| 372 | <div align ="left"><t able cells pacing="4" cellpaddi ng="0" bor der="0"><t r><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td><t d bgcolor= "#023264" width="1" height="1" ><img src= "./images/ void.gif" alt="" wid th="1" hei ght="1" vs pace="0" h space="0" border="0" ></td></tr ><tr><td b gcolor="#0 23264" wid th="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#ffff ff" height ="1"><pre> | |
| 373 | Context in itContext = new Init ialContext (); | |
| 374 | Context en vContext = (Context )initConte xt.lookup( "java:/com p/env"); | |
| 375 | DataSource ds = (Dat aSource)en vContext.l ookup("jdb c/myoracle "); | |
| 376 | Connection conn = ds .getConnec tion(); | |
| 377 | //etc. | |
| 378 | </pre></td ><td bgcol or="#02326 4" width=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td></ tr><tr><td bgcolor=" #023264" w idth="1" h eight="1"> <img src=" ./images/v oid.gif" a lt="" widt h="1" heig ht="1" vsp ace="0" hs pace="0" b order="0"> </td><td b gcolor="#0 23264" hei ght="1"><i mg src="./ images/voi d.gif" alt ="" width= "1" height ="1" vspac e="0" hspa ce="0" bor der="0"></ td><td bgc olor="#023 264" width ="1" heigh t="1"><img src="./im ages/void. gif" alt=" " width="1 " height=" 1" vspace= "0" hspace ="0" borde r="0"></td ></tr></ta ble></div> | |
| 379 | </blockquo te></td></ tr></table > | |
| 380 | ||
| 381 | ||
| 382 | <table bor der="0" ce llspacing= "0" cellpa dding="2"> <tr><td bg color="#82 8DA6"><fon t color="# ffffff" fa ce="arial, helvetica. sanserif"> <a name="P ostgreSQL" ><strong>P ostgreSQL< /strong></ a></font>< /td></tr>< tr><td><bl ockquote> | |
| 383 | <h3>0. Introducti on</h3> | |
| 384 | <p>Postgre SQL is con figured in a similar manner to Oracle.</ p> | |
| 385 | ||
| 386 | <h3>1. Req uired file s </h3> | |
| 387 | <p> | |
| 388 | Copy the P ostgres JD BC jar to $CATALINA_ HOME/lib. As with Or acle, the | |
| 389 | jars need to be in t his direct ory in ord er for DBC P's Classl oader to f ind | |
| 390 | them. This has to be done rega rdless of which conf iguration step you t ake next. | |
| 391 | </p> | |
| 392 | ||
| 393 | <h3>2. Res ource conf iguration< /h3> | |
| 394 | ||
| 395 | <p> | |
| 396 | You have t wo choices here: def ine a data source tha t is share d across a ll Tomcat | |
| 397 | applicatio ns, or def ine a data source spe cifically for one ap plication. | |
| 398 | </p> | |
| 399 | ||
| 400 | <h4>2a. Sh ared resou rce config uration</h 4> | |
| 401 | <p> | |
| 402 | Use this o ption if y ou wish to define a datasource that is s hared acro ss | |
| 403 | multiple T omcat appl ications, or if you just prefe r defining your data source | |
| 404 | in this fi le. | |
| 405 | </p> | |
| 406 | <p><i>This author ha s not had success he re, althou gh others have repor ted so. | |
| 407 | Clarificat ion would be appreci ated here. </i></p> | |
| 408 | ||
| 409 | <div align ="left"><t able cells pacing="4" cellpaddi ng="0" bor der="0"><t r><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td><t d bgcolor= "#023264" width="1" height="1" ><img src= "./images/ void.gif" alt="" wid th="1" hei ght="1" vs pace="0" h space="0" border="0" ></td></tr ><tr><td b gcolor="#0 23264" wid th="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#ffff ff" height ="1"><pre> | |
| 410 | <Resour ce name="j dbc/postgr es" auth=" Container" | |
| 411 | type="java x.sql.Data Source" dr iverClassN ame="org.p ostgresql. Driver" | |
| 412 | url="jdbc: postgresql ://127.0.0 .1:5432/my db" | |
| 413 | username=" myuser" pa ssword="my passwd" ma xActive="2 0" maxIdle ="10" maxW ait="-1"/& gt; | |
| 414 | </pre></td ><td bgcol or="#02326 4" width=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td></ tr><tr><td bgcolor=" #023264" w idth="1" h eight="1"> <img src=" ./images/v oid.gif" a lt="" widt h="1" heig ht="1" vsp ace="0" hs pace="0" b order="0"> </td><td b gcolor="#0 23264" hei ght="1"><i mg src="./ images/voi d.gif" alt ="" width= "1" height ="1" vspac e="0" hspa ce="0" bor der="0"></ td><td bgc olor="#023 264" width ="1" heigh t="1"><img src="./im ages/void. gif" alt=" " width="1 " height=" 1" vspace= "0" hspace ="0" borde r="0"></td ></tr></ta ble></div> | |
| 415 | <h4>2b. Ap plication- specific r esource co nfiguratio n</h4> | |
| 416 | ||
| 417 | <p> | |
| 418 | Use this o ption if y ou wish to define a datasource specific to your ap plication, | |
| 419 | not visibl e to other Tomcat ap plications . This met hod is les s invasive to your | |
| 420 | Tomcat ins tallation. | |
| 421 | </p> | |
| 422 | ||
| 423 | <p> | |
| 424 | Create a r esource de finition f or your <a href="con fig/contex t.html">Co ntext</a>. | |
| 425 | The Contex t element should loo k somethin g like the following . | |
| 426 | </p> | |
| 427 | ||
| 428 | <div align ="left"><t able cells pacing="4" cellpaddi ng="0" bor der="0"><t r><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td><t d bgcolor= "#023264" width="1" height="1" ><img src= "./images/ void.gif" alt="" wid th="1" hei ght="1" vs pace="0" h space="0" border="0" ></td></tr ><tr><td b gcolor="#0 23264" wid th="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#ffff ff" height ="1"><pre> | |
| 429 | <Contex t> | |
| 430 | ||
| 431 | <Resour ce name="j dbc/postgr es" auth=" Container" | |
| 432 | type="java x.sql.Data Source" dr iverClassN ame="org.p ostgresql. Driver" | |
| 433 | url="jdbc: postgresql ://127.0.0 .1:5432/my db" | |
| 434 | username=" myuser" pa ssword="my passwd" ma xActive="2 0" maxIdle ="10" | |
| 435 | maxWait="- 1"/> | |
| 436 | </Conte xt> | |
| 437 | </pre></td ><td bgcol or="#02326 4" width=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td></ tr><tr><td bgcolor=" #023264" w idth="1" h eight="1"> <img src=" ./images/v oid.gif" a lt="" widt h="1" heig ht="1" vsp ace="0" hs pace="0" b order="0"> </td><td b gcolor="#0 23264" hei ght="1"><i mg src="./ images/voi d.gif" alt ="" width= "1" height ="1" vspac e="0" hspa ce="0" bor der="0"></ td><td bgc olor="#023 264" width ="1" heigh t="1"><img src="./im ages/void. gif" alt=" " width="1 " height=" 1" vspace= "0" hspace ="0" borde r="0"></td ></tr></ta ble></div> | |
| 438 | ||
| 439 | <h3>3. web .xml confi guration</ h3> | |
| 440 | <div align ="left"><t able cells pacing="4" cellpaddi ng="0" bor der="0"><t r><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td><t d bgcolor= "#023264" width="1" height="1" ><img src= "./images/ void.gif" alt="" wid th="1" hei ght="1" vs pace="0" h space="0" border="0" ></td></tr ><tr><td b gcolor="#0 23264" wid th="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#ffff ff" height ="1"><pre> | |
| 441 | <resour ce-ref> | |
| 442 | <descr iption> postgreSQL Datasourc e example& lt;/descri ption> | |
| 443 | <res-r ef-name> ;jdbc/post gres</r es-ref-nam e> | |
| 444 | <res-t ype>jav ax.sql.Dat aSource< ;/res-type > | |
| 445 | <res-a uth>Con tainer< /res-auth& gt; | |
| 446 | </resou rce-ref> ; | |
| 447 | </pre></td ><td bgcol or="#02326 4" width=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td></ tr><tr><td bgcolor=" #023264" w idth="1" h eight="1"> <img src=" ./images/v oid.gif" a lt="" widt h="1" heig ht="1" vsp ace="0" hs pace="0" b order="0"> </td><td b gcolor="#0 23264" hei ght="1"><i mg src="./ images/voi d.gif" alt ="" width= "1" height ="1" vspac e="0" hspa ce="0" bor der="0"></ td><td bgc olor="#023 264" width ="1" heigh t="1"><img src="./im ages/void. gif" alt=" " width="1 " height=" 1" vspace= "0" hspace ="0" borde r="0"></td ></tr></ta ble></div> | |
| 448 | ||
| 449 | <h4>4. Acc essing the datasourc e</h4> | |
| 450 | <p> | |
| 451 | When acces sing the d atasource programmat ically, re member to prepend | |
| 452 | <code>java :/comp/env </code> to your JNDI lookup, a s in the f ollowing s nippet of | |
| 453 | code. Note also that "jdbc/pos tgres" can be replac ed with an y value yo u prefer, provided | |
| 454 | you change it in the above res ource defi nition fil e as well. | |
| 455 | </p> | |
| 456 | ||
| 457 | <div align ="left"><t able cells pacing="4" cellpaddi ng="0" bor der="0"><t r><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td><t d bgcolor= "#023264" width="1" height="1" ><img src= "./images/ void.gif" alt="" wid th="1" hei ght="1" vs pace="0" h space="0" border="0" ></td></tr ><tr><td b gcolor="#0 23264" wid th="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#ffff ff" height ="1"><pre> | |
| 458 | InitialCon text cxt = new Initi alContext( ); | |
| 459 | if ( cxt = = null ) { | |
| 460 | throw n ew Excepti on("Uh oh -- no cont ext!"); | |
| 461 | } | |
| 462 | ||
| 463 | DataSource ds = (Dat aSource) c xt.lookup( "java:/co mp/env/jdb c/postgres " ); | |
| 464 | ||
| 465 | if ( ds == null ) { | |
| 466 | throw n ew Excepti on("Data s ource not found!"); | |
| 467 | } | |
| 468 | </pre></td ><td bgcol or="#02326 4" width=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td></ tr><tr><td bgcolor=" #023264" w idth="1" h eight="1"> <img src=" ./images/v oid.gif" a lt="" widt h="1" heig ht="1" vsp ace="0" hs pace="0" b order="0"> </td><td b gcolor="#0 23264" hei ght="1"><i mg src="./ images/voi d.gif" alt ="" width= "1" height ="1" vspac e="0" hspa ce="0" bor der="0"></ td><td bgc olor="#023 264" width ="1" heigh t="1"><img src="./im ages/void. gif" alt=" " width="1 " height=" 1" vspace= "0" hspace ="0" borde r="0"></td ></tr></ta ble></div> | |
| 469 | ||
| 470 | </blockquo te></td></ tr></table > | |
| 471 | </blockquo te></td></ tr></table ><table bo rder="0" c ellspacing ="0" cellp adding="2" ><tr><td b gcolor="#5 25D76"><fo nt color=" #ffffff" f ace="arial ,helvetica .sanserif" ><a name=" Non-DBCP S olutions"> <!--()-->< /a><a name ="Non-DBCP _Solutions "><strong> Non-DBCP S olutions</ strong></a ></font></ td></tr><t r><td><blo ckquote> | |
| 472 | <p> | |
| 473 | These solu tions eith er utilise a single connection to the da tabase (no t recommen ded for an ything oth er | |
| 474 | than testi ng!) or so me other p ooling tec hnology. | |
| 475 | </p> | |
| 476 | </blockquo te></td></ tr></table ><table bo rder="0" c ellspacing ="0" cellp adding="2" ><tr><td b gcolor="#5 25D76"><fo nt color=" #ffffff" f ace="arial ,helvetica .sanserif" ><a name=" Oracle 8i with OCI c lient"><!- -()--></a> <a name="O racle_8i_w ith_OCI_cl ient"><str ong>Oracle 8i with O CI client< /strong></ a></font>< /td></tr>< tr><td><bl ockquote> | |
| 477 | <table bor der="0" ce llspacing= "0" cellpa dding="2"> <tr><td bg color="#82 8DA6"><fon t color="# ffffff" fa ce="arial, helvetica. sanserif"> <a name="O racle 8i w ith OCI cl ient/Intro duction">< !--()--></ a><a name= "Oracle_8i _with_OCI_ client/Int roduction" ><strong>I ntroductio n</strong> </a></font ></td></tr ><tr><td>< blockquote > | |
| 478 | <p>Whilst not strict ly address ing the cr eation of a JNDI Dat aSource us ing the OC I client, these note s can be c ombined wi th the | |
| 479 | Oracle and DBCP solu tion above .</p> | |
| 480 | <p> | |
| 481 | In order t o use OCI driver, yo u should h ave an Ora cle client installed . You shou ld have in stalled | |
| 482 | Oracle8i(8 .1.7) clie nt from cd , and dow nload the suitable J DBC/OCI | |
| 483 | driver(Ora cle8i 8.1. 7.1 JDBC/O CI Driver) from <a h ref="http: //otn.orac le.com/">o tn.oracle. com</a>. | |
| 484 | </p> | |
| 485 | <p> | |
| 486 | After rena ming <code >classes12 .zip</code > file to <code>clas ses12.jar< /code> | |
| 487 | for Tomcat , copy it into <code >$CATALINA _HOME/lib< /code>. | |
| 488 | You may al so have to remove th e <code>ja vax.sql.*< /code> cla sses | |
| 489 | from this file depen ding upon the versio n of Tomca t and JDK you are us ing. | |
| 490 | </p> | |
| 491 | </blockquo te></td></ tr></table > | |
| 492 | ||
| 493 | <table bor der="0" ce llspacing= "0" cellpa dding="2"> <tr><td bg color="#82 8DA6"><fon t color="# ffffff" fa ce="arial, helvetica. sanserif"> <a name="P utting it all togeth er"><!--() --></a><a name="Putt ing_it_all _together" ><strong>P utting it all togeth er</strong ></a></fon t></td></t r><tr><td> <blockquot e> | |
| 494 | <p> | |
| 495 | Ensure tha t you have the <code >ocijdbc8. dll</code> or <code> .so</code> in your < code>$PATH </code> or <code>LD_ LIBRARY_PA TH</code> | |
| 496 | (possibly in <code> $ORAHOME\b in</code>) and also confirm th at the nat ive librar y can be l oaded by a simple te st program | |
| 497 | using <cod e>System.l oadLibrary ("ocijdbc8 ");</code> | |
| 498 | </p> | |
| 499 | <p> | |
| 500 | You should next crea te a simpl e test ser vlet or js p that has these | |
| 501 | <strong>cr itical lin es</strong >: | |
| 502 | </p> | |
| 503 | <div align ="left"><t able cells pacing="4" cellpaddi ng="0" bor der="0"><t r><td bgco lor="#0232 64" width= "1" height ="1"><img src="./ima ges/void.g if" alt="" width="1" height="1 " vspace=" 0" hspace= "0" border ="0"></td> <td bgcolo r="#023264 " height=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td><t d bgcolor= "#023264" width="1" height="1" ><img src= "./images/ void.gif" alt="" wid th="1" hei ght="1" vs pace="0" h space="0" border="0" ></td></tr ><tr><td b gcolor="#0 23264" wid th="1"><im g src="./i mages/void .gif" alt= "" width=" 1" height= "1" vspace ="0" hspac e="0" bord er="0"></t d><td bgco lor="#ffff ff" height ="1"><pre> | |
| 504 | DriverMana ger.regist erDriver(n ew | |
| 505 | oracle.jdb c.driver.O racleDrive r()); | |
| 506 | conn = | |
| 507 | DriverMana ger.getCon nection("j dbc:oracle :oci8:@dat abase","us ername","p assword"); | |
| 508 | </pre></td ><td bgcol or="#02326 4" width=" 1"><img sr c="./image s/void.gif " alt="" w idth="1" h eight="1" vspace="0" hspace="0 " border=" 0"></td></ tr><tr><td bgcolor=" #023264" w idth="1" h eight="1"> <img src=" ./images/v oid.gif" a lt="" widt h="1" heig ht="1" vsp ace="0" hs pace="0" b order="0"> </td><td b gcolor="#0 23264" hei ght="1"><i mg src="./ images/voi d.gif" alt ="" width= "1" height ="1" vspac e="0" hspa ce="0" bor der="0"></ td><td bgc olor="#023 264" width ="1" heigh t="1"><img src="./im ages/void. gif" alt=" " width="1 " height=" 1" vspace= "0" hspace ="0" borde r="0"></td ></tr></ta ble></div> | |
| 509 | <p> | |
| 510 | where data base is of the form <code>host :port:SID< /code> Now if you tr y to acces s the URL of your | |
| 511 | test servl et/jsp and what you get is a | |
| 512 | <code>Serv letExcepti on</code> with a roo t cause of <code>jav a.lang.Uns atisfiedLi nkError:ge t_env_hand le</code>. | |
| 513 | </p> | |
| 514 | <p> | |
| 515 | First, the <code>Uns atisfiedLi nkError</c ode> indic ates that you have | |
| 516 | <ul> | |
| 517 | <li>a mism atch betwe en your JD BC classes file and | |
| 518 | your Oracl e client v ersion. Th e giveaway here is t he message stating t hat a need ed library file cann ot be | |
| 519 | found. For example, you may be using a c lasses12.z ip file fr om Oracle Version 8. 1.6 with a Version 8 .1.5 | |
| 520 | Oracle cli ent. The c lasseXXXs. zip file a nd Oracle client sof tware vers ions must match. | |
| 521 | </li> | |
| 522 | <li>A <cod e>$PATH</c ode>, <cod e>LD_LIBRA RY_PATH</c ode> probl em.</li> | |
| 523 | <li>It has been repo rted that ignoring t he driver you have d ownloded f rom otn an d using | |
| 524 | the classe s12.zip fi le from th e director y <code>$O RAHOME\jdb c\lib</cod e> will al so work. | |
| 525 | </li> | |
| 526 | </ul> | |
| 527 | </p> | |
| 528 | <p> | |
| 529 | Next you m ay experie nce the er ror <code> ORA-06401 NETCMN: in valid driv er designa tor</code> | |
| 530 | </p> | |
| 531 | <p> | |
| 532 | The Oracle documenta tion says : "Cause: The login (connect) string con tains an i nvalid | |
| 533 | driver des ignator. A ction: Cor rect the s tring and re-submit. " | |
| 534 | ||
| 535 | Change the database connect st ring (of t he form <c ode>host:p ort:SID</c ode>) with this one: | |
| 536 | <code>(des cription=( address=(h ost=myhost )(protocol =tcp)(port = PORT ))(connect _data=(sid =orcl)))</ code> | |
| 537 | </p> | |
| 538 | <p> | |
| 539 | <i>Ed. Hmm , I don't think this is really needed if you sort out your T NSNames - but I'm no t an Oracl e DBA :-)< /i> | |
| 540 | </p> | |
| 541 | </blockquo te></td></ tr></table > | |
| 542 | </blockquo te></td></ tr></table ><table bo rder="0" c ellspacing ="0" cellp adding="2" ><tr><td b gcolor="#5 25D76"><fo nt color=" #ffffff" f ace="arial ,helvetica .sanserif" ><a name=" Common Pro blems"><!- -()--></a> <a name="C ommon_Prob lems"><str ong>Common Problems< /strong></ a></font>< /td></tr>< tr><td><bl ockquote> | |
| 543 | <p>Here ar e some com mon proble ms encount ered with a web appl ication wh ich | |
| 544 | uses a dat abase and tips for h ow to solv e them.</p > | |
| 545 | ||
| 546 | <table bor der="0" ce llspacing= "0" cellpa dding="2"> <tr><td bg color="#82 8DA6"><fon t color="# ffffff" fa ce="arial, helvetica. sanserif"> <a name="I ntermitten t Database Connectio n Failures "><!--()-- ></a><a na me="Interm ittent_Dat abase_Conn ection_Fai lures"><st rong>Inter mittent Da tabase Con nection Fa ilures</st rong></a>< /font></td ></tr><tr> <td><block quote> | |
| 547 | <p> | |
| 548 | Tomcat run s within a JVM. The JVM perio dically pe rforms gar bage colle ction | |
| 549 | (GC) to re move java objects wh ich are no longer be ing used. When the JVM | |
| 550 | performs G C executio n of code within Tom cat freeze s. If the maximum ti me | |
| 551 | configured for estab lishment o f a databa se connect ion is les s than the amount | |
| 552 | of time ga rbage coll ection too k you can get a data base conne ction fail ure. | |
| 553 | </p> | |
| 554 | ||
| 555 | <p>To coll ect data o n how long garbage c ollection is taking add the | |
| 556 | <code>-ver bose:gc</c ode> argum ent to you r <code>CA TALINA_OPT S</code> | |
| 557 | environmen t variable when star ting Tomca t. When v erbose gc is enabled | |
| 558 | your <code >$CATALINA _BASE/logs /catalina. out</code> log file will inclu de | |
| 559 | data for e very garba ge collect ion includ ing how lo ng it took .</p> | |
| 560 | ||
| 561 | <p>When yo ur JVM is tuned corr ectly 99% of the tim e a GC wil l take les s | |
| 562 | than one s econd. Th e remainde r will onl y take a f ew seconds . Rarely, | |
| 563 | if ever sh ould a GC take more than 10 se conds.</p> | |
| 564 | ||
| 565 | <p>Make su re that th e db conne ction time out is set to 10-15 seconds. | |
| 566 | For the DB CP you set this usin g the para meter <cod e>maxWait< /code>.</p > | |
| 567 | ||
| 568 | </blockquo te></td></ tr></table > | |
| 569 | ||
| 570 | <table bor der="0" ce llspacing= "0" cellpa dding="2"> <tr><td bg color="#82 8DA6"><fon t color="# ffffff" fa ce="arial, helvetica. sanserif"> <a name="R andom Conn ection Clo sed Except ions"><!-- ()--></a>< a name="Ra ndom_Conne ction_Clos ed_Excepti ons"><stro ng>Random Connection Closed Ex ceptions</ strong></a ></font></ td></tr><t r><td><blo ckquote> | |
| 571 | <p> | |
| 572 | These can occur when one reque st gets a db connect ion from t he connect ion | |
| 573 | pool and c loses it t wice. Whe n using a connection pool, clo sing the | |
| 574 | connection just retu rns it to the pool f or reuse b y another request, | |
| 575 | it doesn't close the connectio n. And To mcat uses multiple t hreads to | |
| 576 | handle con current re quests. He re is an e xample of the sequen ce | |
| 577 | of events which coul d cause th is error i n Tomcat: | |
| 578 | </p> | |
| 579 | <pre> | |
| 580 | Request 1 running in Thread 1 gets a d b connecti on. | |
| 581 | ||
| 582 | Request 1 closes t he db conn ection. | |
| 583 | ||
| 584 | The JVM switches t he running thread to Thread 2 | |
| 585 | ||
| 586 | Request 2 running in Thread 2 gets a d b connecti on | |
| 587 | (the sam e db conne ction just closed by Request 1 ). | |
| 588 | ||
| 589 | The JVM switches t he running thread ba ck to Thre ad 1 | |
| 590 | ||
| 591 | Request 1 closes t he db conn ection a s econd time in a fina lly block. | |
| 592 | ||
| 593 | The JVM switches t he running thread ba ck to Thre ad 2 | |
| 594 | ||
| 595 | Request 2 Thread 2 tries to use the db connectio n but fail s | |
| 596 | because Request 1 closed it. | |
| 597 | </pre> | |
| 598 | <p> | |
| 599 | Here is an example o f properly written c ode to use a databas e connecti on | |
| 600 | obtained f rom a conn ection poo l: | |
| 601 | </p> | |
| 602 | <pre> | |
| 603 | Connecti on conn = null; | |
| 604 | Statemen t stmt = n ull; // O r Prepared Statement if needed | |
| 605 | ResultSe t rs = nul l; | |
| 606 | try { | |
| 607 | conn = ... get c onnection from conne ction pool ... | |
| 608 | stmt = conn.crea teStatemen t("select ..."); | |
| 609 | rs = s tmt.execut eQuery(); | |
| 610 | ... it erate thro ugh the re sult set . .. | |
| 611 | rs.clo se(); | |
| 612 | rs = n ull; | |
| 613 | stmt.c lose(); | |
| 614 | stmt = null; | |
| 615 | conn.c lose(); // Return to connectio n pool | |
| 616 | conn = null; // Make sure we don't close it t wice | |
| 617 | } catch (SQLExcept ion e) { | |
| 618 | ... de al with er rors ... | |
| 619 | } finall y { | |
| 620 | // Alw ays make s ure result sets and statements are close d, | |
| 621 | // and the conne ction is r eturned to the pool | |
| 622 | if (rs != null) { | |
| 623 | try { rs.close (); } catc h (SQLExce ption e) { ; } | |
| 624 | rs = null; | |
| 625 | } | |
| 626 | if (st mt != null ) { | |
| 627 | try { stmt.clo se(); } ca tch (SQLEx ception e) { ; } | |
| 628 | stmt = null; | |
| 629 | } | |
| 630 | if (co nn != null ) { | |
| 631 | try { conn.clo se(); } ca tch (SQLEx ception e) { ; } | |
| 632 | conn = null; | |
| 633 | } | |
| 634 | } | |
| 635 | </pre> | |
| 636 | ||
| 637 | </blockquo te></td></ tr></table > | |
| 638 | ||
| 639 | <table bor der="0" ce llspacing= "0" cellpa dding="2"> <tr><td bg color="#82 8DA6"><fon t color="# ffffff" fa ce="arial, helvetica. sanserif"> <a name="C ontext ver sus Global NamingReso urces"><!- -()--></a> <a name="C ontext_ver sus_Global NamingReso urces"><st rong>Conte xt versus GlobalNami ngResource s</strong> </a></font ></td></tr ><tr><td>< blockquote > | |
| 640 | <p> | |
| 641 | Please n ote that a lthough th e above in structions place the JNDI decl arations i n a Contex t | |
| 642 | element, it is pos sible and sometimes desirable to place t hese decla rations in the | |
| 643 | <a href= "config/gl obalresour ces.html"> GlobalNami ngResource s</a> sect ion of the server | |
| 644 | configur ation file . A resou rce placed in the Gl obalNaming Resources section wi ll be shar ed | |
| 645 | among th e Contexts of the se rver. | |
| 646 | </p> | |
| 647 | </blockquo te></td></ tr></table > | |
| 648 | ||
| 649 | <table bor der="0" ce llspacing= "0" cellpa dding="2"> <tr><td bg color="#82 8DA6"><fon t color="# ffffff" fa ce="arial, helvetica. sanserif"> <a name="J NDI Resour ce Naming and Realm Interactio n"><!--()- -></a><a n ame="JNDI_ Resource_N aming_and_ Realm_Inte raction">< strong>JND I Resource Naming an d Realm In teraction< /strong></ a></font>< /td></tr>< tr><td><bl ockquote> | |
| 650 | <p> | |
| 651 | In order to get Re alms to wo rk, the re alm must r efer to th e datasour ce as | |
| 652 | defined in the < ;GlobalNam ingResourc es> or <Contex t> sect ion, not a datasourc e as renam ed | |
| 653 | using &l t;Resource Link>. | |
| 654 | </p> | |
| 655 | </blockquo te></td></ tr></table > | |
| 656 | ||
| 657 | </blockquo te></td></ tr></table ></td></tr ><!--FOOTE R SEPARATO R--><tr><t d colspan= "2"><hr no shade="nos hade" size ="1"></td> </tr><!--P AGE FOOTER --><tr><td colspan=" 2"><div al ign="cente r"><font c olor="#525 D76" size= "-1"><em> | |
| 658 | Co pyright &c opy; 1999- 2013, Apac he Softwar e Foundati on | |
| 659 | </ em></font> </div></td ></tr></ta ble></body ></html> |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.