Spring+SpringMVC+MyBatis+easyUI Integration Basics (7) Connection parameters of JDBC url

  In the programming of the connection between the java program and the database, the mysql jdbc url format is as follows:
jdbc:mysql://[host:port],[host:port].../[database][?parameter name1][=parameter value1][¶meter name2][=parameter value2].. .
如 jdbc:mysql://localhost:3306/test?user=test&password=123456
 
  I just recently encountered a problem caused by a database connection parameter, so here are a few more important parameters:
 
  user The  database user name (used to connect to the database) Required parameter.
 
  password  User password (used to connect to the database) If a password is set, it is also a necessary parameter.
 
   Whether useUnicode  uses the Unicode character set, if the parameter characterEncoding is set to utf-8 or gbk, the value of this parameter needs to be set to true, and the default is false.
 
   characterEncoding  allows the user to set the database encoding and specify the character encoding. When the program interacts with the database, if the encoding type of the data is inconsistent with the encoding type of the database, such as GBK is used in the program, and the data type of the database is utf8, a transfer will appear. The parameter could not identify the problem that resulted in not getting the expected data return. To fix this, we need to set characterEncoding=utf8 on the URL.
  Sample code:
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
  
  autoReconnect  Whether to automatically reconnect when the database connection is interrupted abnormally, the default value is false.
 
  Whether autoReconnectForPools  uses the reconnection strategy for the database connection pool, the default value is false.
  The above two parameters are to set whether to allow automatic reconnection after the connection is disconnected. The connection of mysql in jdbc may be disconnected or the database suddenly goes down after a long connection. Restarting the database service may continue to report exceptions. Only by restarting the application and reconnecting can avoid continuing to report errors. In order to solve this problem, this parameter is generally set. In this case, you can use this parameter to ask the jdbc driver to report an abnormal database connection. Reconnect automatically. In the case of using data connection pool, such as using DBCP or c3p0 connection pool, you should try to use autoReconnectForPools. 
 
  The timeout when connectTimeout  establishes a socket connection with the database server, in milliseconds, 0 means never timeout.
 
  socketTimeout  socket operation (read and write) timeout, unit: milliseconds, 0 means never timeout.
  JDBC uses sockets to connect to the database. The database does not handle the connection timeout between the application and the database. The socket timeout of JDBC is very important when the database is suddenly stopped or a network error occurs (due to equipment failure, etc.). Due to the structure of TCP/IP, there is no way for sockets to detect network errors, so applications cannot actively find out that the database connection is disconnected. If the socket timeout is not set, the application will wait indefinitely before the database returns the result. This connection is called a dead connection. In order to avoid dead connections, the socket must have a timeout configuration. Socket timeout can be set through JDBC. Socket timeout can avoid the situation of endless waiting of the application when a network error occurs, and shorten the time of service failure.
   This parameter is set for the expiration time of jdbc, which is not the same parameter as the expiration time of mysql-server.

  • The timeout when socket is connected: set by Socket.connect(SocketAddress endpoint, int timeout)
  • The timeout when socket is read and written: set by Socket.setSoTimeout(int timeout)

  Sample code:

jdbc:mysql://localhost:3306/test?connectTimeout=60000&socketTimeout=60000  

 

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326430192&siteId=291194637