Data Source (DataSource)

The data source (DataSource)
is the standard interface developed by SUN to obtain the database connection.
It exists in the javax.sql package and is used instead of DriverManager
to obtain connections.

The difference between DataSource and DriverManager for obtaining connections:
a). DriverManager is implemented by SUN, which only provides the most basic way to obtain
   connections.
   DataSource is an interface, which can be implemented not only by SUN
   , but also by many third-party middleware, and it is well implemented.

DataSource generally has the following three implementation methods:
  standard implementation--provides the most basic connection, that is, the implementation of the connection pool in the form of DriverManager
  --provides a connection pool, which is a
    "container" that can cache and manage multiple database connections .
  The realization of distributed transaction -- provides connection pool, and the connection in this pool
  supports distributed transaction (Distribute Transaction)

Generally speaking, DataSource will be implemented by professional middleware (MiddleWare).
Of course, there are also some open source (free) implementations of DataSource.
The most famous is: commons-dbcp provided by the Apache organization

--

It is recommended that DataSource be used to obtain connections in the future.


The relational data source (DataSource) between the data source and the connection pool is a specification made by SUN (the specification for obtaining database connections),

Connection pooling is a relatively mainstream and important implementation of the data source specification.

Use the commons-dbcp component to build the data source
It depends on:
   commons-dbcp-1.4.jar
   commons-pool-1.6.jar
two jar files.

Core class:
BasicDataSource implements the javax.sql.DataSource interface.

--

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/ja0218
username=root
password=1234 #Maximum
number of connections: The maximum number of active connections that the connection pool can allocate at the same time ,, if it is set to a non-positive number, it means no limit, the default value is 8
maxActive=15 #Minimum
idle connection: The minimum number of connections allowed to remain idle in the connection pool, below this number, a new connection will be created, if set to 0 It will not be created, the default value is 0
minIdle=5 #Maximum
idle connection: the maximum number of connections in the connection pool that are allowed to remain idle, the excess idle connections will be released, if set to a negative number, it means no limit, the default value is 8
maxIdle=10
# Number of initialized connections: The number of initialized connections created when the connection pool starts, the default value is 0
initialSize=5 #Whether to print logAbandoned=true
when the connection is leaked #Whether to automatically recycle the timeout connection removeAbandoned=true



#Timeout (in seconds)
removeAbandonedTimeout=180 #Maximum
wait time: The maximum time (in milliseconds) that the connection pool waits for a connection to be returned when there is no available connection, throw an exception if it exceeds the time, if set to - 1 means infinite wait, the default value is infinite
maxWait=3000 #The
time value (in milliseconds) to sleep during the idle connection collector thread running.
timeBetweenEvictionRunsMillis=10000 #Check
every time the idle connection collector thread (if any) runs The number of connections
numTestsPerEvictionRun=8 #The
connection remains idle in the pool and not by the idle connection recycler thread
minEvictableIdleTimeMillis=10000 #Used
to verify the connection taken from the connection pool
validationQuery=SELECT 1 #Indicate
whether to check before taking the connection from the pool
testOnBorrow=true
#testOnReturn false indicates whether to test before returning to the pool
testOnReturn=true




Guess you like

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