Spring+springmvc+mybatis+easyui Integration Basics (vii) connection parameters for JDBC URLs

Source: Internet
Author: User
Tags connection pooling

In programming Java programs and database connections, the MySQL JDBC URL format is as follows: jdbc:mysql://[host:port],[host:port].../[database][? parameter name 1][= argument value 1][& argument Name 2] [= parameter value 2] ... such as jdbc:mysql://localhost:3306/test?user=test&password=123456 just recently encountered a database connection parameters caused by the problem, so list a few more important parameters:UserDatabase user name (used to connect to database) necessary parameters.PasswordThe user password (used to connect to the database) is also required if a password is set.    UseunicodeWhether to use the Unicode character set, if the parameter characterencoding is set to Utf-8 or GBK, the value of this parameter is set to TRUE and the default is False.    characterencoding allows users to set their own database encoding, specify character encoding, when the program interacts with the database, if the data encoding type is inconsistent with the encoding type of the database, such as the use of GBK in the program, The data type of the database is UTF8, and there is an issue where the parameters cannot be recognized, causing the data to be returned without being expected. To resolve this issue, we need to set the Characterencoding=utf8 on the URL. Example code:
Jdbc:mysql://localhost:3306/test? useunicode=true&characterencoding=utf8
  AutoReConnectWhether to automatically reconnect when the database connection is interrupted abnormally, the default value is False.AutoreconnectforpoolsWhether to use a reconnection policy against the database connection pool, the default value is False. The above two parameters are set whether to allow automatic reconnection after disconnection, JDBC in the connection of MySQL after a long time connection, there may be a disconnection or a sudden database outage situation. Restarting the database service may also continue to report the exception, only restart the app reconnect to avoid continuing the error, in order to solve this problem, it is generally set this parameter,This parameter can then be used to require the JDBC driver to automatically reconnect after discovering a database connection exception。 If you use a data connection pool, such as using DBCP or C3P0 connection pooling, you should use Autoreconnectforpools as much as possible. ConnectTimeoutTimeout when establishing a socket connection with the database server, in milliseconds, 0 means never time out.SockettimeoutSocket operation (Read-write) timeout, in milliseconds, 0 means never time out. JDBC uses a socket to connect to a database, and the database does not process the connection timeout between the application and the database, and JDBC's socket timeout is important when the database is suddenly stopped or a network error occurs due to a device failure. Because of the structure of TCP/IP, the socket has no way to detect a network error, so the application cannot proactively discover that the database connection is broken. If the socket timeout is not set, the application will wait indefinitely until the database returns results, which is known as dead connection. In order to avoid dead connections,socket must have a timeout configuration. The socket timeout can be set through JDBC, and the socket timeout avoids the endless wait when a network error occurs, shortening the time of service failure.This parameter is the JDBC Expiration time setting, which is not the same as the Mysql-server expiration time.

Timeout when socket connected: set by Socket.connect (socketaddress endpoint, int timeout)
socket timeout When reading and writing: set by socket.setsotimeout (int timeout)

Example code:

Jdbc:mysql://localhost:3306/test? connecttimeout=60000&sockettimeout=60000  

The above is the more commonly used parameters, if interested can go to the official website to check the other parameters, thank you for watching.

Spring+springmvc+mybatis+easyui Integration Basics (vii) connection parameters for JDBC URLs

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.