相关疑难解决方法(0)

配置hibernate以通过JNDI Datasource连接到数据库

大家好我正在使用Hibernate + Struts2 + Tomcat6 + Mysql作为我的J2EE框架.我一直在使用hibernate的内置连接池机制,但结果证明mysql在8小时后关闭它的连接是有问题的.无论如何,我google了一下,发现我应该通过JNDI数据源获得我的连接,但我无法就此达成一个完整的教程.我应该采取什么措施呢?请给我足够的细节,我对此有点新意.这是我的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.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">
            jdbc:mysql://localhost/hposg?characterEncoding=UTF-8
    </property>
    <property name="connection.username">root</property>
    <property name="connection.password"></property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="current_session_context_class">thread</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="hbm2ddl.auto">update</property>

    <property name="hibernate.max_fetch_depth">3</property>

    <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    <property name="hibernate.c3p0.acquire_increment">2</property>
    <property name="hibernate.c3p0.idle_test_period">300</property>
    <property name="hibernate.c3p0.timeout">1800</property>
    <property name="hibernate.c3p0.max_size">25</property>
    <property name="hibernate.c3p0.min_size" >3</property>
    <property name="hibernate.c3p0.max_statement">0</property>
    <property name="hibernate.c3p0.preferredTestQuery">select 1;</property>
    <property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
    <property name="hibernate.c3p0.validate">true</property>

      <!-- Mapping files -->

      <mapping resource="com/hposg/hibernate/resources/Player.hbm.xml"/>
      <mapping resource="com/hposg/hibernate/resources/Game.hbm.xml"/>
      ...

  </session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)

我也尝试了这个,但它没有成功:在WebContent/META-INF中创建一个context.xml,包含:

<Context path="" docBase="../RSGames" debug="0" …
Run Code Online (Sandbox Code Playgroud)

java hibernate jndi connection-pooling

28
推荐指数
2
解决办法
12万
查看次数

标签 统计

connection-pooling ×1

hibernate ×1

java ×1

jndi ×1