日期:2014-05-18  浏览次数:20806 次

【急】请教高手:关于Hibernate配置Tomcat数据源问题?
请教高手:

我想使用Tomcat 5.5.9配置数据源,然后Hibernate3.0使用JNDI获取SessionFactory,然后在Dao中使用这个SessionFactory获取Session,执行save或者update操作,最后在Action中调用Dao的方法。

我想按照这个思路实现。

我的一些配置资料如下:
context.xml为:
XML code

<Context path="/HibernateDataSource" docBase="HibernateDataSource"
        debug="5" reloadable="true" crossContext="true">

  <Resource name="jdbc/myhibernate"
                auth="Container"
                type="javax.sql.DataSource"
                maxActive="100"
                maxIdle="30"
                maxWait="10000"
                username="sa"
                password="111111"
                driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
                url="jdbc:microsoft:sqlserver://localhost:1433;databasename=person"/>

</Context>


HibernateService.java如下:
Java code

public SessionFactory getSessionFactory(){
        SessionFactory sf = null;
        String jndi = "jdbc/myhibernate";
        try {
            //Context ctx = new InitialContext();
            //sf = (SessionFactory)ctx.lookup(jndi);System.out.println("sf===="+sf);
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            sf = (SessionFactory) envCtx.lookup(jndi);
        } catch (NamingException e) {
            e.printStackTrace();
        }    
        return sf;
    }


hibernate.cfg.xml如下:
XML code

<?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>
<!-- http://hao.com/html/down/index.html -->
<session-factory name="jdbc/myhibernate">
    <!--<property name="connection.username">sa</property>
        <property name="connection.url">
        jdbc:microsoft:sqlserver://localhost:1433;databasename=person
        </property>
        <property name="dialect">
        org.hibernate.dialect.SQLServerDialect
        </property>
        <property name="myeclipse.connection.profile">MSSQL</property>
        <property name="connection.password">111111</property>    
        <property name="hibernate.connection.pool.size">50</property>
        
    -->
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
    <property name="connection.datasource">
        java:comp/env/jdbc/myhibernate
    </property>
    <property name="show_sql">true</property>
    <property name="jndi.url"></property>

    <mapping resource="org/shirdrn/entity/Person.hbm.xml" />

</session-factory>

</hibernate-configuration>



Servlet的代码如下所示:
Java code

public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws 
                         myeclipse6.5 浏览器报错但是控制台不报错解决方案