In a word, spring's integration of Hibernate is implemented in Applicationcontext.xml configuration sessionfactory , in which sessionfactory to assemble datasource. The configuration of the DataSource Bean is described in detail below.
The Sessionfactory interface is responsible for initializing hibernate. It acts as a proxy for the data storage source and is responsible for creating session objects. The factory model is used here. It is important to note that Sessionfactory is not lightweight, because in general, a project typically requires only one sessionfactory, and when you need to manipulate multiple databases, you can specify a sessionfactory for each database. In fact its designer's intention is to allow it to be shared throughout the application. Sessionfactory actually played a buffer in hibernate, buffering hibernate's automatically generated SQL statements and some other mapping data, and buffering some data that might be reused in the future. The session is created by Sessionfactory and can create many sessions. The session interface is one of the most important interfaces for hibernate developers. in the minds of hibernate designers, they regard the session as a middle interface between data connection and transaction management. We can think of the session as a buffer of persistent objects, hibernate detects changes to these persistent objects, and refreshes the database in a timely manner. We sometimes call the session a persistent Layer manager because it contains some persistent layer-related operations, such as storing persistent objects to the database, and getting them from the database.
The DataSource data source is injected into the sessionfactory and then associated to the session. First the code (the more complete attribute):
<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" ><property Name= "Driverclassname" value= "${driverclassname}"/><property name= "url" value= "${url}"/><property name = "username" value= "${username}"/><property name= "password" value= "${password}"/><!--connection initial value, Initial value of the number of connections created when connection pooling starts--><property name= "initialsize" value= "${initialsize}"/><!--the maximum value of the connection pool, The maximum number of connections that can be allocated from the pool at the same time, 0 o'clock Unlimited--><property name= "maxactive" value= "${maxactive}"/><!-- Maximum idle value. After a rush hour, the connection pool slowly releases a portion of the connection that has not been used, and has been reduced to maxidle until 0 o'clock unrestricted--><property name= "Maxidle" value= "${maxidle} "/><!--minimum idle value. When the number of idle connections is less than the threshold, the connection pool will be pre-applied to some connections to avoid the flood peak time to apply--><property name=" Minidle "value=" ${minidle} "/ ><!--Whether the prepared statement is pooled (Boolean), PreparedStatement is cached--><property name= "poolpreparedstatements" value= " True "/><!--whether to commit the SQL automatically--><property name=" Defaultautocommit "value=" true "/></bean>
The 1.Bean ID is datasource, and the corresponding Java class is Basicdatasource, which is suitable for configuring the base type of database connection, if you want to make a multi-data source, Then the Java class for the bean will have to be dynamicdatasource.
2.destroy-method, which means that the Close method in the Basicdatasource class is called when the connection is destroyed.
3.driverclassname,url,username,password are basic configurations, and the value values of these properties are introduced through an external property profile, and how to introduce them in detail, see
The 3rd propertyconfigurer in http://blog.csdn.net/dreamrealised/article/details/9123199.
4. The following properties are related to database connection pooling, see http://blog.csdn.net/dreamrealised/article/details/9127563 for the basics of database connection pooling
1) initialsize, connection initial value, initial value of the number of connections created when connection pooling starts
2) maxactive, maximum connection pool, the maximum number of connections that can be allocated from the pool at the same time, 0 o'clock Unlimited
3) Maxidle, maximum idle value. After a peak time, the connection pool can slowly release a portion of the connection that has not been used, until it has been reduced to maxidle, until 0 o'clock Unlimited
4) Minidle, minimum idle value. When the number of idle connections is less than the threshold, the connection pool will be pre-applied to some connections to avoid the flood peak when the application is too late
5) Poolpreparedstatements, whether the prepared statement is pooled (Boolean), whether the PreparedStatement is cached
6) Defaultautocommit, whether to automatically commit SQL, transaction management often to turn off the JDBC automatic submission function, transaction management see
http://blog.csdn.net/dreamrealised/article/details/9123173
. Configuring a Dynamic Data source
Add the Dynamicdatasource bean to the spring context XML configuration file and configure the map map for the Dynamicdatasource targetdatasources (multiple data source target) attribute.
<bean id= "Dynamicdatasource" class= "datasource. Dynamicdatasource ">
<property name= "Targetdatasources" >
<map>
<entry value-ref= "Datasourcefrom" key= "Datasourcefrom" ></entry>
<entry value-ref= "Datasourceto" key= "Datasourceto" ></entry>
</map>
</property>
<property name= "Defaulttargetdatasource" ref= "Datasourcefrom"/>
</bean>
In the example named DataSource, this data source is configured as a dynamic data source, and its default managed data source can be configured in its Targetdatasources property. We modify the name of the data source for the underlying operation of the platform to Datasource1 and delegate the default hold management to the data source.
Spring's DataSource Configuration "Go"