【急】请教高手:关于Hibernate配置Tomcat数据源问题?
请教高手:
我想使用Tomcat 5.5.9配置数据源,然后Hibernate3.0使用JNDI获取SessionFactory,然后在Dao中使用这个SessionFactory获取Session,执行save或者update操作,最后在Action中调用Dao的方法。
我想按照这个思路实现。
我的一些配置资料如下:
context.xml为:
<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>
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; }
<?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>
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!!!"); } }
public boolean savePerson(Person p)throws MyException{ beginTransaction(); session.save(p); endTransaction(true); return true; }
<%@ 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>