【非Web工程】Spring + C3P0 + Hibernate + Mysql

工程依赖包:


配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:magpie="http://www.unionpay.com/schema/magpie" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.unionpay.com/schema/magpie  http://www.unionpay.com/schema/magpie/magpie.xsd" default-lazy-init="false">

	<context:annotation-config />
    
    <context:component-scan base-package="com.up.tsm"/>
	
	<import resource="applicationContext-db.xml" />
</beans>
配置文件applicationContext-db.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
           http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">

		<!-- 指定连接数据库的驱动 -->
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<!-- 指定连接数据库的URL -->
		<property name="jdbcUrl" value="jdbc:mysql:loadbalance://IP1:端口2,IP2:端口2/数据库?roundRobinLoadBalance=true" />

		<!-- 指定连接数据库的用户名 -->
		<property name="user" value="用户名" />
		<!-- 指定连接数据库的密码 -->
		<property name="password" value="密码" />

		<!-- 指定连接数据库连接池的最大连接数 -->
		<property name="maxPoolSize" value="100" />
		<!-- 指定连接数据库连接池的最小连接数 -->
		<property name="minPoolSize" value="100" />
		<!-- 指定连接数据库连接池的初始化连接数 -->
		<property name="initialPoolSize" value="100" />
		<property name="checkoutTimeout" value="2000" />
		<property name="maxStatements" value="0" />
		<property name="maxStatementsPerConnection" value="0" />
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan" value="com.cup" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider
				</prop>
				<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
				<prop key="hibernate.connection.useUnicode">true</prop>
				<prop key="hibernate.cache.use_second_level_cache">false</prop>
				<prop key="hibernate.cache.use_query_cache">false</prop>
				<prop key="hibernate.show_sql">none</prop>
				<prop key="hibernate.hbm2ddl.auto">none</prop>
				<prop key="hibernate.format_sql">none</prop>
			</props>
		</property>
	</bean>

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>

	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 增加Annotation方式的事务配置 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
启动代码:

package com.up.tsm.svr.data;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 服务启动类
 * @author biankai
 *
 */
public class BootStrap 
{
    public static void main( String[] args )
    {
    	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    	
    }
}

启动后发现C3P0在两台数据库服务器上随机建立若干个连接,连接总数=初始连接池大小。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值