Hibernate配置数据库连接

1. Hibernate默认连接池

2.C3P0连接池

3.Proxool连接池

4.JNDIDataSource

1. Hibernate默认连接池
Xml代码 Hibernate配置数据库连接 Hibernate配置数据库连接Hibernate配置数据库连接
  1. <session-factory>  
  2.     <property name="hibernate.connection.driver_class">  
  3.         com.mysql.jdbc.Driver   
  4.     </property>  
  5.     <property name="hibernate.connection.url">  
  6.         jdbc:mysql://127.0.0.1:3306/txazo   
  7.     </property>  
  8.     <property name="hibernate.connection.username">root</property>  
  9.     <property name="hibernate.connection.password">root</property>  
  10.   
  11.     <property name="hibernate.connection.pool_size">10</property>  
  12.   
  13.     <property name="hibernate.dialect">  
  14.         org.hibernate.dialect.MySQLDialect   
  15.     </property>  
  16.   
  17.     <property name="hibernate.current_session_context_class">thread</property>  
  18.   
  19.     <property name="hibernate.show_sql">true</property>  
  20.     <property name="hibernate.format_sql">true</property>  
  21.     <property name="hibernate.hbm2ddl.auto">update</property>  
  22.   
  23.     <mapping class="com.txazo.domain.User" />  
  24. </session-factory>  
<session-factory>
	<property name="hibernate.connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
	<property name="hibernate.connection.url">
        jdbc:mysql://127.0.0.1:3306/txazo
    </property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password">root</property>

    <property name="hibernate.connection.pool_size">10</property>

	<property name="hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
    </property>

	<property name="hibernate.current_session_context_class">thread</property>

	<property name="hibernate.show_sql">true</property>
	<property name="hibernate.format_sql">true</property>
	<property name="hibernate.hbm2ddl.auto">update</property>

	<mapping class="com.txazo.domain.User" />
</session-factory>

2. C3P0连接池

Xml代码 Hibernate配置数据库连接 Hibernate配置数据库连接Hibernate配置数据库连接
  1. <session-factory>  
  2.     <property name="hibernate.connection.driver_class">  
  3.         com.mysql.jdbc.Driver   
  4.     </property>  
  5.     <property name="hibernate.connection.url">  
  6.         jdbc:mysql://127.0.0.1:3306/txazo   
  7.     </property>  
  8.     <property name="hibernate.connection.username">root</property>  
  9.     <property name="hibernate.connection.password">root</property>  
  10.   
  11.     <property name="hibernate.c3p0.min_size">5</property>  
  12.     <property name="hibernate.c3p0.max_size">20</property>  
  13.     <property name="hibernate.c3p0.timeout">1000</property>  
  14.     <property name="hibernate.c3p0.max_statements">100</property>  
  15.   
  16.     <property name="hibernate.dialect">  
  17.         org.hibernate.dialect.MySQLDialect   
  18.     </property>  
  19.   
  20.     <property name="hibernate.current_session_context_class">thread</property>  
  21.   
  22.     <property name="hibernate.show_sql">true</property>  
  23.     <property name="hibernate.format_sql">true</property>  
  24.     <property name="hibernate.hbm2ddl.auto">update</property>  
  25.   
  26.     <mapping class="com.txazo.domain.User" />  
  27. </session-factory>  
<session-factory>
	<property name="hibernate.connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
	<property name="hibernate.connection.url">
        jdbc:mysql://127.0.0.1:3306/txazo
    </property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password">root</property>

    <property name="hibernate.c3p0.min_size">5</property>
	<property name="hibernate.c3p0.max_size">20</property>
	<property name="hibernate.c3p0.timeout">1000</property>
	<property name="hibernate.c3p0.max_statements">100</property>

	<property name="hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
    </property>

	<property name="hibernate.current_session_context_class">thread</property>

	<property name="hibernate.show_sql">true</property>
	<property name="hibernate.format_sql">true</property>
	<property name="hibernate.hbm2ddl.auto">update</property>

	<mapping class="com.txazo.domain.User" />
</session-factory>

3.Proxool连接池

1)添加Proxool的JAR包

2)src/proxool.xml
Xml代码 Hibernate配置数据库连接 Hibernate配置数据库连接Hibernate配置数据库连接
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <something-else-entirely>  
  4.     <proxool>  
  5.         <alias>txazo</alias>  
  6.         <driver-url>jdbc:mysql://127.0.0.1:3306/txazo</driver-url>  
  7.         <driver-class>com.mysql.jdbc.Driver</driver-class>  
  8.         <driver-properties>  
  9.             <property name="user" value="root" />  
  10.             <property name="password" value="root" />  
  11.         </driver-properties>  
  12.         <minimum-connection-count>5</minimum-connection-count>  
  13.         <maximum-connection-count>20</maximum-connection-count>  
  14.     </proxool>  
  15. </something-else-entirely>  
<?xml version="1.0" encoding="UTF-8"?>

<something-else-entirely>
	<proxool>
		<alias>txazo</alias>
		<driver-url>jdbc:mysql://127.0.0.1:3306/txazo</driver-url>
		<driver-class>com.mysql.jdbc.Driver</driver-class>
		<driver-properties>
			<property name="user" value="root" />
			<property name="password" value="root" />
		</driver-properties>
		<minimum-connection-count>5</minimum-connection-count>
		<maximum-connection-count>20</maximum-connection-count>
	</proxool>
</something-else-entirely>

3)src/hibernate.cfg.xml

Xml代码 Hibernate配置数据库连接 Hibernate配置数据库连接Hibernate配置数据库连接
  1. <session-factory>  
  2.     <property name="hibernate.connection.provider_class">  
  3.         org.hibernate.connection.ProxoolConnectionProvider   
  4.     </property>  
  5.     <property name="hibernate.proxool.pool_alias">txazo</property>  
  6.     <property name="hibernate.proxool.xml">proxool.xml</property>  
  7.   
  8.     <property name="hibernate.dialect">  
  9.         org.hibernate.dialect.MySQLDialect   
  10.     </property>  
  11.   
  12.     <property name="hibernate.current_session_context_class">thread</property>  
  13.   
  14.     <property name="hibernate.show_sql">true</property>  
  15.     <property name="hibernate.format_sql">true</property>  
  16.     <property name="hibernate.hbm2ddl.auto">update</property>  
  17.   
  18.     <mapping class="com.txazo.domain.User" />  
  19. </session-factory>  
<session-factory>
    <property name="hibernate.connection.provider_class">
        org.hibernate.connection.ProxoolConnectionProvider
    </property>
    <property name="hibernate.proxool.pool_alias">txazo</property>
	<property name="hibernate.proxool.xml">proxool.xml</property>

	<property name="hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
    </property>

	<property name="hibernate.current_session_context_class">thread</property>

	<property name="hibernate.show_sql">true</property>
	<property name="hibernate.format_sql">true</property>
	<property name="hibernate.hbm2ddl.auto">update</property>

	<mapping class="com.txazo.domain.User" />
</session-factory>

4. JNDI DataSource

Xml代码 Hibernate配置数据库连接 Hibernate配置数据库连接Hibernate配置数据库连接
  1. <session-factory>  
  2.     <property name="hibernate.connection.datasource">  
  3.         java:comp/env/jdbc/txazo   
  4.     </property>  
  5.   
  6.     <property name="hibernate.dialect">  
  7.         org.hibernate.dialect.MySQLDialect   
  8.     </property>  
  9.   
  10.     <property name="hibernate.current_session_context_class">thread</property>  
  11.   
  12.     <property name="hibernate.show_sql">true</property>  
  13.     <property name="hibernate.format_sql">true</property>  
  14.     <property name="hibernate.hbm2ddl.auto">update</property>  
  15.   
  16.     <mapping class="com.txazo.domain.User" />  
  17. </session-factory>  

相关推荐