Configuration method of DataSource in Tomcat

Source: Internet
Author: User
Tags commit copy interface mysql resource tomcat
A lot of online articles are talking about changing server.xml, including one of my "Eclipse from getting started to mastering." However, after today's nightmare-like experience, eventually did not change Server.xml, the datasource configuration well. In the middle of the countless detours, including the Tomcat from 5.5.9 to 5.0.28 (Ultimately, there is no use of 5.5.9, the surrounding people say, do not use the latest things ... The lab is using 4.1 ... )。

First of all about Tomcat 5.5.9, because the Tomcat5.5.9 did not take the admin package, must be under their own, after the next copy to the corresponding directory. There is another important step: Delete the admin directory under root .... or rename that admin\index.html or delete it ... This problem tortured me for more than half an hour, just can't find out the admin configuration page where ...
Strangely enough, there was no jsp/html file in the Root\admin directory, which made me almost think I'd missed something. In fact, it seems that if you delete the admin directory, Tomcat automatically points to another place. After entering the view, you can find that in fact/admin point to/server/webapps/admin, but there is no jsp/html file, the real address in:/src/jakarta-tomcat-catalina/webapps/ Admin ... So, we must remember that index.html to kill ...

But after the successful entry, I have been unable to configure the Jdbc/mysql datasource ... Extremely depressed, plus others say the latest version is always a bug ... I changed back to the 5.0.28 ...
5.0.28 directly into the admin on it, do not have to go so many detours. There are two ways to configure DataSource, one is directly to the context (/jsp file directory) below the data sources configuration, configuration is basically very clear (for example, MySQL):
JNDI Name:jdbc/mysql (own set)
Data Source url:jdbc:mysql://localhost/test (test is database name)
JDBC Driver Class:com.mysql.jdbc.Driver (remember to copy the drive to Common\lib)
Max. Active connections: Maximum active connection, default 4, can not be used for product release, release, change to 1000 or so better, but when debugging is not necessary.
Max. Idle connections: Maximum idle connection, default 2.
Max. Wait for Connection: Waiting for connection time, default 5000.
Validation query: Verify that you are using a lookup statement, not required.
Then save, Commit.
Here's a bug. It is generally best to commit after all modifications have been completed. If the first commit and then make other choices to modify, generally will directly exit the interface ... I usually save/commit/log out ...
Then reference in the program:
InitialContext ctx=new InitialContext ();
DataSource ds= (DataSource) ctx.lookup ("Java:comp/env/jdbc/mysql"); Jdbc/mysql is Jndi Name
con = ds.getconnection ();
The other is the same, note that before returning to manually shut down the database connection, or soon the number of connections exhausted.

Another approach is to set the DataSource in the global resource and then refer to resource links in the context.
The Resource link setting method is as follows:
Name:jdbc/mysql (can be different from the global name, such as JJ, other invariant, corresponding, ctx.lookup to "JAVA:COMP/ENV/JJ")
Global:jdbc/mysql (the name of data source in the global resource)
Type:javax.sql.DataSource
There are some problems with the eclipse from getting started to mastering: you can set it directly in resource link and do not have to change the server.xml directly. It is written in front of the method to write Server.xml without deploy Context,tomcat confirm the update after the context to write to other places, can be directly modified in host.contest inside, increase resource Link.
The handwriting server.xml in E Book achieves the following statements to increase the context effect of non-Tomcat directories:
Insert before the end of the Server.xml file:
</context path= "/hello"
reloadable= "true"
docbase= "C:\eclipse\workspace\myweb\" Hello "
workdir=" C:\eclipse\workspace\myweb\bin ">

In fact, you can directly in the admin interface inside TomcatServer.Service.Host directly add (note that the use naming set to True, otherwise the name Java:comp will not be bound in this Context exception), but in this case the servlet output directory (WORKDIR) cannot be specified. Can be weighed after use. Personally I think it is convenient to set up the admin interface directly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.