MyEclipse中使用Proxool+mysql连接池的方法

来源:互联网 发布:人工智能之父 图灵 编辑:程序博客网 时间:2024/06/05 14:07

该例子是针对一下情形使用:

(1)使用的是mysql数据库;

(2)适用于java应用程序,如果是web程序还需要修改web.xml。

首先,新建proxoolconf.xml文件,页面内容如下:

<?xml version="1.0" encoding="UTF-8"?> <!-- the proxool configuration can be embedded within your own application's. Anything outside the "proxool" tag is ignored. --> <something-else-entirely> <proxool> <!--连接池的别名--> <alias>DBPool</alias>   <!--proxool只能管理由自己产生的连接--> <driver-url> jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF8 </driver-url> <!—JDBC驱动程序--> <driver-class>com.mysql.jdbc.Driver</driver-class> <driver-properties> <property name="user" value="root"/> <property name="password" value=""/> </driver-properties>   <!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回 收,超时的销毁--> <house-keeping-sleep-time>90000</house-keeping-sleep-time>   <!-- 指因未有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的 用户连接就不会被接受--> <maximum-new-connections>20</maximum-new-connections>   <!-- 最少保持的空闲连接数--> <prototype-count>5</prototype-count>   <!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的 等待请求数由maximum-new-connections决定--> <maximum-connection-count>100</maximum-connection-count>   <!-- 最小连接数--> <minimum-connection-count>10</minimum-connection-count>   </proxool> </something-else-entirely>


然后,针对hibernate.cfg.xml文件进行修改成如下:

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   --><hibernate-configuration><session-factory><property name="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property><property name="hibernate.proxool.pool_alias">DBPool</property><property name="hibernate.proxool.xml">proxoolconf.xml</property><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK</property><property name="connection.username">user</property><property name="connection.password">password</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="myeclipse.connection.profile">tx</property><property name="hibernate.current_session_context_class">thread</property><mapping resource="dao/User.hbm.xml" /></session-factory></hibernate-configuration>

这样,配置就已经完成了。

写一个测试程序:

UserDAO daydao = new UserDAO();               JAXPConfigurator.configure(CONFIG_FILE_LOCATION, false);
Transaction tran = daydao.getSession().beginTransaction();User day = new User();User temp = daydao.findById("wang");if (temp == null){day.setUsername("wang");day.setPassword("123456");try {daydao.save(day);tran.commit();} catch (Exception e){}}List findAll = daydao.findAll();java.util.List<User> d = findAll;for (User o:d){System.out.print(o.getUsername() + "  ");System.out.print(o.getPassword() + "  ");System.out.println();}


能够成功运行,OK了。