首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

:关于Hibernate配置Tomcat数据源有关问题

2011-12-20 
【急】请教高手:关于Hibernate配置Tomcat数据源问题?请教高手:我想使用Tomcat 5.5.9配置数据源,然后Hibernat

【急】请教高手:关于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 ServletException, IOException {        Person p = new Person();        p.setUserid(new Integer(1288));        p.setUsername("异域王者");        p.setGender("男");        p.setMajor("DSS");        p.setPhone("13843140000");        p.setEmail("shirdrn@hotmail.com");        boolean boo = PersonService.getPersonDao().savePerson(p);        if(boo){            System.out.println("success!!!");        }    } 



PersonDao.java的save方法为:
Java code
public boolean savePerson(Person p)throws MyException{        beginTransaction();        session.save(p);        endTransaction(true);        return true;    }


使用SQL Server 2000,而且Tomcat可以正常启动,执行添加save操作的时候报异常:
java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource
org.shirdrn.util.HibernateService.getSessionFactory(HibernateService.java:34)
org.shirdrn.util.HibernateService.beginTransaction(HibernateService.java:42)
org.shirdrn.dao.PersonDao.savePerson(PersonDao.java:13)
org.shirdrn.action.SavePersonAction.doPost(SavePersonAction.java:30)
org.shirdrn.action.SavePersonAction.doGet(SavePersonAction.java:17)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

请高手指点一下,感激不尽!!!

[解决办法]
楼主我用你的context.xml配置数据源
我测试了一下,没有任何问题,不过我没有用hibernate进去测试,测试的代码如下,你可以参考下 :
HTML code
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ page import="java.sql.*,javax.naming.*,javax.sql.*" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><%    Context ctx = new InitialContext();    DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/connectionPool");    Connection con = ds.getConnection();    Statement stmt = con.createStatement();    ResultSet rs = stmt.executeQuery("select t.xxx from myTable t");    while (rs.next()) {        String name = rs.getString("xxx");        out.println("name:" + name);    }    rs.close();    stmt.close();    con.close();%><body></body></html> 

热点排行