hibernate 与 jdbc 共用一个proxool池

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">

<hibernate-configuration>

	<session-factory>


			<property name="connection.provider_class">
				org.hibernate.connection.DriverManagerConnectionProvider
			</property>
			<property name="connection.url">proxool.DBPool</property>


	</session-factory>

</hibernate-configuration>
 

proxool.xml 配置

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

<something-else-entirely>
	<proxool>
		<alias>DBPool</alias>
		 <!-- autoReconnect=true 自动提交 -->
		<driver-url>jdbc:mysql://localhost:3306/database?autoReconnect=true</driver-url>
		<driver-class>com.mysql.jdbc.Driver</driver-class>
		<driver-properties>
			<property name="user" value="root" />
			<property name="password" value="root" />
		</driver-properties>

		<house-keeping-sleep-time>900000</house-keeping-sleep-time>
		<max-new-connections>20</max-new-connections>
		<max-connection-count>50</max-connection-count>
		<min-connection-count>20</min-connection-count>
		<prototype-count>5</prototype-count>
	</proxool>
</something-else-entirely>
 

java 类 获得jdbc连接

public class DBUtil {
	private static final Log log = LogFactory.getLog(DBUtil.class);
	private static boolean inited = false;
	static {
		try {
			Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
			inited = true;
		} catch (Exception e) {
			log.error(e);
		}

	}
	
	/**
	 * 建立jdbc数据库连接
	 * 
	 * @return connection
	 */
	public static Connection  getConnection () {
		Connection conn = null;
		if (inited) {
			try {
				conn = DriverManager.getConnection("proxool.DBPool");
			} catch (SQLException e) {
				log.error(e);
			}
		}
		return conn;
	}
	
	
	/**
	 * 关闭数据库连接
	 * @param c 连接
	 */
	public static void close(Connection c) {
		if (c != null) {
			try {
				c.close();
			} catch (SQLException e) {
				log.error(e);
			}
		}
	}
}
 

猜你喜欢

转载自w54653520.iteye.com/blog/1522793
今日推荐