In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to correctly use Tomcat data sources, many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
1. The function and operation principle of data source.
The use of data sources in program code can improve operational performance, which depends on the principle of operation.
Traditional JDBC operation procedure
1. Load the database driver, which is configured through CLASSPATH
2. Get the database connection object through the DriverManager class
3. Instantiate PreparedStatement objects through Connection and write SQL commands to operate the database.
4. The database belongs to the resource operation. After the operation is completed, the database is closed to release the resources. As shown in the figure:
Only the operation is different for different users, but it is obviously a repetitive operation for steps 1, 2, and 4.
If you use JDBC operations directly in your development, then this performance problem will arise, so what is the most appropriate way to do this?
If it is assumed that the database is not closed, in the future, if a new user uses it, it will directly take an existing connection.
For example, the school provides umbrellas for students, and umbrellas will be prepared for students once it rains. At this time, students do not have to look for umbrellas again, and then buy umbrellas again.
Suppose there are 100 umbrellas, if it doesn't rain now, we certainly can't put all the umbrellas on them, so we usually put at least 10 umbrellas if no one is using them. Of course, only 100 umbrellas can be provided at the maximum.
One more waiting time is needed.
The minimum number of database connections maintained and the maximum number of connections allowed to be opened.
This operation has been supported since Tomcat version 4.1, which is called database connection pooling, where all database connections are stored.
2. Use database connection pool in Tomcat
In the web container, the connection pool of the database is accessed through the data source (javax.sql.DataSource), that is, you can get the Connection object through the javax.sql.DataSource class, but if you want to get a DataSource object, you need to use JNDI to find it.
JNDI (Java Naming and Directory Interface) belongs to the naming and directory lookup interface, and its main function is to find objects.
However, the connection pool for the current database needs to be configured on Tomcat.
You have to modify the server.xml file to work.
As follows, take connecting to mysql as an example:
There are several parameters for this configuration:
Name: represents the name of the data source, which is also the name that JNDI is looking for
Auth: indicates who is responsible for resource connection. Container: container management, application: program management, generally set to Container
Type: represents an object, and every binding on the data source is DataSource.
MaxActive: indicates the maximum number of activated connections. A value of 100indicates that there are at most 100 database connections at the same time. Generally, maxActive is set to a possible concurrency.
MaxIdle: indicates the maximum number of idle connections. The value here is 30, which means that 30 idle connections can be maintained even if there is no database connection, without being cleared and on standby at any time.
MaxWait: indicates the maximum number of seconds to wait. A value of 10000 indicates a timeout after 10 seconds. A value of-1 means to wait indefinitely until the timeout. An exception will be received if the timeout occurs.
Username: database user name
Password: database login password
DriverClassName: database driver name
.url: database url
But now you are using a version of Tomcat that is later than 6.0, so if you want a data source to work, you must also configure it in the web.xml (note: this web.xml is the web.xml file of the web project, not the web.xml file of the tomcat server).
Jdbc/mydb javax.sql.DataSource Container
3. Find the data source
The operation of the data source uses JNDI for lookup, so if you want to use the data source to obtain a database connection, you must follow these steps
Initialize name lookup context: Context ctx = new InitialContext ()
Find the DataSource object by name: DataSource ds = (DataSource) ctx.lookup (JNDI name)
Get a database connection through DataSource: Connection conn = ds.getConnection ().
An Exception appears when the database is called:
Javax.servlet.ServletException: javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
In fact, for this kind of resource operation, it needs an environment attribute support: java:comp/env, but the Tomcat server itself is free, and there is no support for this attribute. If you want to access the name service in Tomcat, you must add this attribute in front, that is, the name now is: java:comp/env/jdbc/mydb; that is Tomcat, then the JNDI name is: java:comp/env/JNDI name.
In the future, only the name will be recognized in the program, and the specific database will be determined by the configuration.
Of course, if you are using the DatabaseConnection.java class developed by DAO.
Package com.shawn.mvcdemo.dbc;import java.sql.*;import javax.sql.*;import javax.naming.*;public class DatabaseConnection {private static final String DSNAME = "java:comp/env/jdbc/mldn"; / / java:comp/JNDI name private Connection conn = null; public DatabaseConnection () throws Exception {Context ctx = new InitialContext (); / / initialize name lookup context DataSource ds = (DataSource) ctx.lookup (DSNAME); / / find DataSource object this.conn = ds.getConnection () by name / / obtain a database connection via DataSource} public Connection getConnection () {return this.conn;} public void close () throws Exception {if (this.conn! = null) {try {this.conn.close (); / / release the database connection} catch (Exception e) {throw e;}} public static void main (String args []) {try {System.out.println (new DatabaseConnection (). GetConnection ()) } catch (Exception e) {e.printStackTrace ();}
However, it must be noted that the current database connection pool is really configured on Tomcat, so this program can only be run under web, not using application programs.
Summary:
To use database connection pooling
1. Configure server.xml
2. Configure the web.xml file in the web project (for example, webdemo project), and add the resource-ref configuration
3. Modify the way to get Connection in the program.
After reading the above, do you have any further understanding of how to use Tomcat data sources correctly? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.