Hibernate配置数据库连接

1. Hibernate默认连接池
2. C3P0连接池
3. Proxool连接池
4. JNDI DataSource

1. Hibernate默认连接池
<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连接池
<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 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
<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
<session-factory>
    <property name="hibernate.connection.datasource">
        java:comp/env/jdbc/txazo
    </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>

你可能感兴趣的:(Hibernate,dataSource)