spring,hibernate,proxool的整合
在hibernate中使用proxool非常简单,只需要把hibernate.cfg.xml中的数据库配置改一改就可以了.
默认的是:
- <hibernate-configuration>
- <session-factory>
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql://200.200.1.249/activemq</property>
- <property name="connection.username">root</property>
- <property name="connection.password">root</property>
- <property name="show_sql">true</property>
- <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
- <mapping resource="test.hbm.xml"/>
- ...(resource)
- </session-factory>
- </hibernate-configuration>
改为:
- <hibernate-configuration>
- <session-factory>
- <property name="proxool.pool_alias">jms1</property>
- <property name="proxool.xml">proxool.xml</property>
- <property name="connection.provider_class">
- org.hibernate.connection.ProxoolConnectionProvider
- </property>
- <property name="show_sql">true</property>
- <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
- <mapping resource="Customer.hbm.xml"/>
- ...(resource)
- </session-factory>
- </hibernate-configuration>
当然,需要写一个proxool的配置文件proxool.xml
new Configuration().configure().buildSessionFactory()打开的SessionFactory调用的是连接池创建的数据库连接.
在spring中使用proxool:
- <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" destroy-method="close">
- <property name="driver" value="com.mysql.jdbc.Driver" />
- <property name="driverUrl" value="jdbc:mysql://localhost/activemq?user=root&password=root" /><!--必须在这里配置用户名/密码,下面的貌似没用-->
- <property name="user" value="root" />
- <property name="password" value="root" />
- <property name="alias" value="jms1" />
- <property name="houseKeepingSleepTime" value="90000" />
- <property name="prototypeCount" value="5" />
- <property name="maximumConnectionCount" value="100" />
- <property name="minimumConnectionCount" value="10" />
- </bean>
再说说spring,hibernate,与proxool的集成
当spring与hibernate整合时,SessionFactory是由org.springframework.orm.hibernate3.LocalSessionFactoryBean来创造的
直接使用jdbc创建数据库连接:
- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost/activemq" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- </bean>
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.cache.use_query_cache">true</prop>
- <!--<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>-->
- </props>
- </property>
- <property name="mappingLocations">
- <list>
- <value>Customer.hbm.xml</value>
- ...(resource)
- </list>
- </property>
- </bean>
使用proxool创建的数据库连接池:
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
- <prop key="connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</prop>
- <prop key="hibernate.proxool.xml">proxool.xml</prop>
- <prop key="hibernate.proxool.pool_alias">jms1</prop>
- </props>
- </property>
- <property name="mappingResources">
- <list>
- <value>Customer.hbm.xml</value>
- ...(resource)
- </list>
- </property>
- </bean>