This afternoon, I was depressed. I configured Tomcat 6.OracleThe database's JNDI is not easy to use. Later I consulted my master and finally got it ready. After that, I came back and thought about it. I was so careless, I didn't see anything clearly written in the doc ...... Write it out as quickly as possible to prevent other people from getting as depressed as they are when encountering this problem.
1. Configure the oracle JDBC driver. Copy classes111.jar (Oracle8i), classes12.jar (Oracle9i), or ojdbc14.jar (Oracle10g) to the $ catalina_home/lib (or % catalina_home %/LIB) directory in windows.
Note: Earlier drivers versions may be *. zip or *. jar files. Tomcat only uses the * .jarfile under the $ catalina_home/lib directory. For this reason, rename the classes111.zipor classes12.zip file as classes111.jar and classes12.jar files, because the JAR file is a zip file, so there is no need to unzip these zip files and then compress them into jar files.
In addition, Oracle9i and later versions should use Oracle. JDBC. oracle driver is not recommended. JDBC. driver. oracledriver, because Oracle has declared oracle. JDBC. driver. oracledriver is no longer recommended, and Oracle will not provide major later versions for Oracle. JDBC. driver. oracledriver service.
2. Modify the TomcatServerThe. xml file contains the following content:
<Context docbase = "niniapp" Path = "/niniapp" reloadable = "true" Source = "org. Eclipse. JST. Jee. SERVER: qprlzy">
<Resource Name = "jndi_niniapp" auth = "Container" type = "javax. SQL. datasource"
Maxactive = "100" maxidle = "30" maxwait = "10000"
Username = "Nini" Password = "Nini" driverclassname = "oracle. JDBC. Driver. oracledriver"
Url = "JDBC: oracle: thin: @ 192.168.2.9: 1521: orcl"/>
</Context>
The name of the data source added here is jndi_niniapp, and username isDatabaseUser name; password is the password corresponding to the user name in the database; driverclassname is the database driver; maxidle is the maximum number of idle times, that is, the maximum idle time of the database connection, exceeds the idle time, the database connection is marked as unavailable and then released. If it is set to 0, there is no limit. maxactive is the maximum number of database connections in the connection pool. If it is set to 0, there is no limit; maxwait is the maximum connection wait time. If it exceeds the set time, an exception is reported. If it is set to-1, there is no limit.
The blog I recommended to me by my master also mentioned that it can be written like this:
<Context docbase = "niniapp" Path = "/qprlzy" reloadable = "true" Source = "org. Eclipse. JST. Jee. SERVER: qprlzy">
<Resource Name = "jndi_niniapp" auth = "Container" type = "javax. SQL. datasource"/>
<Resourceparams name = "jndi_niniapp">
<Parameter>
<Name> factory </Name>
<Value> org. Apache. commons. DBCP. basicperformancefactory </value>
</Parameter>
<Parameter>
<Name> driverclassname </Name>
<Value> oracle. JDBC. Driver. oracledriver </value>
</Parameter>
<Parameter>
<Name> URL </Name>
<Value> JDBC: oracle: thin: @ 192.168.2.9: 1521: orcl </value>
</Parameter>
<Parameter>
<Name> username </Name>
<Value> Nini </value>
</Parameter>
<Parameter>
<Name> password </Name>
<Value> Nini </value>
</Parameter>
<Parameter>
<Name> maxactive </Name>
<Value> 20 </value>
</Parameter>
<Parameter>
<Name> maxidle </Name>
<Value> 10 </value>
</Parameter>
<Parameter>
<Name> maxwait </Name>
<Value> 10000 </value>
</Parameter>
</Resourceparams>
<Resourcelink global = "jndi_niniapp" name = "jndi_niniapp" type = "javax. SQL. datasource"/>
</Context>
I have tried. Both writing methods can be successful. Of course, I think there are still TomcatWebApplicationManagementInterface (Tomcat web server administration
Tool) to configure the JNDI method, but I have not tried it.
3. Some people say that they need to configure web. xml. I don't need to configure it. Some guys say that if I don't configure it, Tomcat 4 will fail to configure the connection pool. Is this a tomcat4 bug? I don't care! However, to configure it, add the following content to E;/niniapp/web/WEB-INF/Web. xml:
<Web-app>
<Resource-ref>
<Description> Oracle datasource example </description>
<Res-ref-Name> JDBC/myoracle </RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
</Resource-ref>
</Web-app>
Here, res-ref-name is the data source name, and the value of the attribute name configured in server. xml above.
4. Sample Code:
Context initcontext = new initialcontext ();
Context envcontext = (context) initcontext. Lookup ("Java:/COMP/ENV ");
Datasource DS = (datasource) envcontext. Lookup ("JDBC/myoracle ");
Connection conn = Ds. getconnection ();
Follow the steps aboveTestFinally passed!