a question in config hibernate in Tomcat
843836Jun 1 2004 — edited Jan 7 2005 I am a newby in hibernate. I was frustrated when configurate the Tomcat when followed the
reference .
The Exception were:
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
root cause
java.lang.UnsupportedOperationException
org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:161)
org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:532)
net.sf.hibernate.connection.DatasourceConnectionProvider.getConnection(DatasourceConnectionProvid
er.java:56)
net.sf.hibernate.impl.BatcherImpl.openConnection(BatcherImpl.java:278)
net.sf.hibernate.impl.SessionImpl.connect(SessionImpl.java:3264)
net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3244)
net.sf.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:40)
net.sf.hibernate.transaction.JDBCTransactionFactory.beginTransaction(JDBCTransactionFactory.java:
19)
net.sf.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:2195)
org.apache.jsp.jsp1_jsp._jspService(jsp1_jsp.java:65)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
What I have done were as follows: I created a new project and Web Module--named quickstart in
JBuilderX,and then copied all the files in src\. to the project.And also I've copied all needed
jar files(Hibernate 2.1.2\lib\*.jar and JDBC Driver for SqlServer 2000) . And modify the file
\src\hibernate.properties liked this:add comments for the HypersonicSQL
## HypersonicSQL
#hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
#hibernate.connection.driver_class org.hsqldb.jdbcDriver
#hibernate.connection.username sa
#hibernate.connection.password
#hibernate.connection.url jdbc:hsqldb:hsql://localhost
#hibernate.connection.url jdbc:hsqldb:test
#hibernate.connection.url jdbc:hsqldb:.
deleted the comments for MS SQL Server
## MS SQL Server
hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect
hibernate.connection.username sa
hibernate.connection.password sa
compiled all the .java files in the directory net. There was no error but prompt of deprivated
methods.
The following are done as the reference says.
1.config DataSource Tomcat 5.0.19
<Context path="/quickstart" docBase="quickstart" reloadable="true">
<Resource name="jdbc/quickstart" scope="Shareable" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/quickstart">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- DBCP database connection settings -->
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=quickstart</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>sa</value>
</parameter>
<!-- DBCP connection pooling options -->
<parameter>
<name>maxWait</name>
<value>3000</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>100</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
</ResourceParams>
</Context>
no problem,I've tested it using a jsp .
2.hibernate.cfg.xml,just modify dialect to SQLServerDialect
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/quickstart</property>
<property name="show_sql">false</property>
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
<!-- Mapping files -->
<mapping resource="Cat.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3.Cat class ,copied from the reference
4.Cat.hbm.xml copied codes.
5.create table in sqlserver 2000.
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CATand
OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[CAT]
GO
CREATE TABLE [dbo].[CAT] (
[cat_id] [char] (32) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[name] [varchar] (16) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[sex] [char] (1) COLLATE Chinese_PRC_CI_AS NULL ,
[weight] [real] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[CAT] WITH NOCHECK ADD
CONSTRAINT [PK_CAT] PRIMARY KEY CLUSTERED
(
[cat_id]
) ON [PRIMARY]
GO
6.HibernateUtil.java,in the package mypack.
package mypack;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: " +
ex.getMessage(), ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
7.jsp for testing
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import="mypack.HibernateUtil"%>
<%@ page import="net.sf.hibernate.examples.quickstart.*"%>
<%@ page import="net.sf.hibernate.*"%>
<%@ page import="net.sf.hibernate.cfg.*"%>
<html>
<head>
<title>
jsp1
</title>
</head>
<body bgcolor="#ffffff">
<h1>
JBuilder Generated JSP
</h1>
<%
Session session1 = HibernateUtil.currentSession();
Transaction tx= session1.beginTransaction();
Cat princess = new Cat();
princess.setName("Princess");
princess.setSex('F');
princess.setWeight(7.4f);
session1.save(princess);
tx.commit();
HibernateUtil.closeSession();
%>
</body>
</html>
8.
visit http://127.0.0.1:8080/quickstart/jsp1.jsp and confronted the exception
thanks.