Hibernate+C3P0+tomcat6+eclipse的配置

 
 

我有我不喜欢MyEclipse的原因,所以很多开发我都喜欢在Eclipse上完成。要做毕业设计了,首先搭建一下我的开发环境。最基本的部分:hibernate+C3P0+Tomcat6的配置/*,第一次把它们组合起来使用哦~所以记录一下我当时的配置过程,算是一种生活的log吧。*/

曾经用过Tomcat的连接池,顺便说一下吧,配置很简单,官网的说明如下:

You will need to define your Datasource in your Context. Here we define a Datasource called myoracle using the thin driver to connect as user scott, password tiger to the sid called mysid. (Note: with the thin driver this sid is not the same as the tnsname). The schema used will be the default schema for the user scott.

Use of the OCI driver should simply involve a changing thin to oci in the URL string.

<Resource name="jdbc/myoracle" auth="Container"
              type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@127.0.0.1:1521:mysid"
              username="scott" password="tiger" maxActive="20" maxIdle="10"
              maxWait="-1"/> 

2. web.xml configuration

You should ensure that you respect the element ordering defined by the DTD when you create you applications web.xml file.

<resource-ref>
 <description>Oracle Datasource example</description>
 <res-ref-name>jdbc/myoracle</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
</resource-ref>

3. Code example

You can use the same example application as above (asuming you create the required DB instance, tables etc.) replacing the Datasource code with something like

Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();
//etc.

///以下内容源自hibernate文档

hibernate本身也自带了一个连接池,Hibernate 自带的连接池算法相当不成熟。它只是为了让你快些上手,并不适合用于产品系统或性能测试中。 出于最佳性能和稳定性考虑你应该使用第三方的连接池。只需要用特定连接池的设置替换 hibernate.connection.pool_size 即可。这将关闭 Hibernate 自带的连接池。例如,你可能会想用 C3P0

C3P0 是一个随 Hibernate 一同分发的开源的 JDBC 连接池,它位于 lib目录下。 如果你设置了 hibernate.c3p0.* 相关的属性,Hibernate将使用 C3P0ConnectionProvider 来缓存 JDBC 连接。如果你更原意使用 Proxool,请参考发行包中的hibernate.properties 并到 Hibernate 网站获取更多的信息。

这是一个使用 C3P0 的 hibernate.properties 样例文件:

hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.url = jdbc:postgresql://localhost/mydatabase
hibernate.connection.username = myuser
hibernate.connection.password = secret
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

为了能在应用程序服务器(application server)中使用 Hibernate,应当总是将 Hibernate 配置成从注册在 JNDI 中的Datasource 处获得连接,你至少需要设置下列属性中的一个:

表 3.2. Hibernate 数据源属性

属性名

用途

hibernate.connection.datasource

数据源 JNDI 名字

hibernate.jndi.url

JNDI 提供者的 URL(可选)

hibernate.jndi.class

JNDI InitialContextFactory (可选)

hibernate.connection.username

数据库用户(可选)

hibernate.connection.password

数据库密码(可选)



这是一个使用应用程序服务器提供的 JNDI 数据源的 hibernate.properties 样例文件:

hibernate.connection.datasource = java:/comp/env/jdbc/test
hibernate.transaction.factory_class = \
    org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_lookup_class = \
    org.hibernate.transaction.JBossTransactionManagerLookup
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

JNDI 数据源获得的 JDBC 连接将自动参与到应用程序服务器中容器管理的事务(container-managed transactions)中去。

任何连接(connection)属性的属性名都要以 "hibernate.connnection" 开头。例如,你可能会使用 hibernate.connection.charSet 来指定 charSet 连接属性。

通过实现 org.hibernate.connection.ConnectionProvider 接口,你可以定义属于你自己的获得JDBC连接的插件策略。通过设置hibernate.connection.provider_class,你可以选择一个自定义的实现。

///hibernate文档引用结束

正如文档中所说的为了能在应用程序服务器(application server)中使用 Hibernate,应当总是将 Hibernate 配置成从注册在 JNDI 中的Datasource 处获得连接”。所以首先应该在容器tomcat中配置一个C3P0连接池,这和在tomcat中配置连接池只有一点小小的区别,只需要将上面的<Resource …. />换成下面的

<Resource acquireIncrement="1" auth="Container" description="DB Connection" driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" factory="org.apache.naming.factory.BeanFactory" jdbcUrl="jdbc:sqlserver://180.85.171.88:2012;DatabaseName=bysj;SelectMethod=cursor" maxPoolSize="4" minPoolSize="2" name="jdbc/bysj" password="cqubysj" type="com.mchange.v2.c3p0.ComboPooledDataSource" user="bysj"/>

然后在hibernate.cfg.xml中添加一句:<property name="hibernate.connection.datasource">java:/comp/env/jdbc/bysj</property>

详细内容如下所示:

<?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 name="SessionFactory">

<!--

<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>

<property name="hibernate.connection.password">cqubysj</property>

<property name="hibernate.connection.url">jdbc:sqlserver://180.85.171.88:2012;DatabaseName=bysj;SelectMethod=cursor</property>

<property name="hibernate.connection.username">bysj</property>

<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>

<property name="hibernate.c3p0.acquire_increment">5</property>

<property name="hibernate.c3p0.idle_test_period">120</property>

<property name="hibernate.c3p0.max_size">20</property>

<property name="hibernate.c3p0.max_statements">100</property>

<property name="hibernate.c3p0.min_size">2</property>

<property name="hibernate.c3p0.timeout">120</property>

-->

<property name="hibernate.connection.datasource">java:/comp/env/jdbc/bysj</property>

<mapping resource="cn/edu/cqu/bysj/test/Student.hbm.xml"/>

</session-factory>

</hibernate-configuration>

基本的配置就是这么多,下面需要做的就是往WEB-INF/lib中添加必须的库(其实这一步我是最先做的)。为项目添加hibernate支持首先要加的就是hibernate3.jar然后就是lib/required中的全部文件。至于是不是全部都需要,我也不是特别清楚,反正required是这个意思。当然了把这一切都做完之后就需要做一番测试,很不幸在测试的时候抛出了异常,具体什么不记得了,反正是根据异常说明又往WEB-INF/lib中加了两个jar文件(slf4j-nop.jarjavax.persistence.jar),然后再测试就成功了。

 
posted @ 2011-03-11 20:02  MagicLetters  阅读(518)  评论(0编辑  收藏  举报