为什么在hibernate中factory = new Configuration().configure().buildSessionFactory();会这么慢

gks_cn 2005-04-12 11:54:47
有没有解决方法没有?
代码如下:
factory = new Configuration().configure().buildSessionFactory();
配置文件如下:
<hibernate-configuration>
<session-factory name="foo">
<mapping resource="struts/advinfo/db/Advisor.hbm.xml"/>
<mapping resource="sys/bm/Duty.hbm.xml"/>
<mapping resource="struts/message/bo/Accessories.hbm.xml"/>
<mapping resource="struts/message/bo/Body.hbm.xml"/>
<mapping resource="struts/message/bo/Send.hbm.xml"/>
</session-factory>
</hibernate-configuration>
...全文
1814 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
gks_cn 2005-04-12
  • 打赏
  • 举报
回复
运行很快。初始化的时候很慢。
jianghuxing 2005-04-12
  • 打赏
  • 举报
回复
一般在初始化的时候就是慢些,hibernate要初始化很多东西,
如果是运行的时候慢,看年看你的session怎么了用了。是不是每次对数据库操作都要创建新的session
jianghuxing 2005-04-12
  • 打赏
  • 举报
回复
factory = new Configuration().configure(“初始化的配置文件”).buildSessionFactory();

还有,你是初始化的时候慢还是在运行的时候慢呢>??
gks_cn 2005-04-12
  • 打赏
  • 举报
回复
是静态的。
jianghuxing 2005-04-12
  • 打赏
  • 举报
回复
应当是这个问题,
还有你在初始化得到factory 是不是静态的?
gks_cn 2005-04-12
  • 打赏
  • 举报
回复
我的服务器是用的jdk1。3是不是这个的问题?
gks_cn 2005-04-12
  • 打赏
  • 举报
回复
控制台信息是:
05-4-12 12:03:34:703 CST] 3cc2d191 Environment I org.hibernate.cfg.Environment Hibernate 3.0
[05-4-12 12:03:34:703 CST] 3cc2d191 Environment I org.hibernate.cfg.Environment loaded properties from resource hibernate.properties: {hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=org.hibernate.dialect.OracleDialect, hibernate.show_sql=true, hibernate.connection.datasource=jdbc/OracleDs}
[05-4-12 12:03:34:703 CST] 3cc2d191 Environment I org.hibernate.cfg.Environment using CGLIB reflection optimizer
[05-4-12 12:03:34:703 CST] 3cc2d191 Environment I org.hibernate.cfg.Environment JVM does not support Statement.getGeneratedKeys()
[05-4-12 12:03:34:703 CST] 3cc2d191 Environment I org.hibernate.cfg.Environment JVM does not support LinkedHasMap, LinkedHashSet - ordered maps and sets disabled
[05-4-12 12:03:34:703 CST] 3cc2d191 Environment I org.hibernate.cfg.Environment using workaround for JVM bug in java.sql.Timestamp
[05-4-12 12:03:34:703 CST] 3cc2d191 Environment I org.hibernate.cfg.Environment using pre JDK 1.4 java.sql.Timestamp handling
[
liubofan2004 2005-04-12
  • 打赏
  • 举报
回复
免费培训信息

本周六(4月16日)软件工程师班免费听讲(可以免费听三天课程)。系列课程由浅入深针
对无软件开发基础,且有志于从事IT行业的人。感兴趣者,请速致电010-51607418定
座。(详情请登陆http://www.zhihuigu.com 和 http://www.zhihuigu.net)

特邀著名IT专家,Microsft,Sun高级技术培训师吕智良先生现场授课。座位有限,有
兴趣者请在本周四之前电话定位。

第一天课程:Windows Server 2003 的配置与管理
课程安排如下:

第一天部分课程安排如下:

PPT1
内容提要
基本概念介绍(硬件/软件/操作系统)
Microsoft操作系统的发展历程
Windows Server 2003的安装

PPT2
软件、硬件、操作系统概念和分类
微软公司(Microsoft)操作系统的发展
家庭用户
DOS (Disk Operating System) – 磁盘操作系统
Windows 3.x – 视窗操作系统
Windows 95 – 视窗95
Windows 98 – 视窗98
Windows ME (Millennium ) – 视窗千年版
Windows XP Home Edition – XP家庭版
商业用户
Windows NT Server/Windows NT Workstation
Windows 2000 Server/Windows 2000 Professional
Windows Server 2003/Windows XP Professional

PPT3
实战项目: 安装 Windows Server 2003 企业版
形式:两人一组,参考书上(P12-P32)协商讨论完成。
提供的资源:
VMVare 5.0 虚拟机系统
Alcohol 120%虚拟光驱软件
Windows Server 2003 企业版镜像磁盘文件
助教现场指导答疑
时间:2个小时
此实战项目的意义
熟悉Windows Server 2003的完整安装过程
熟悉使用VMVare 5.0虚拟机软件系统
熟悉使用Alcohol 120%虚拟光驱软件
增加实际动手能力和团队协作能力

PPT4
本章总结

本章主要介绍一些基本的概念,了解 Microsoft Windows 一系列操作系统的发展
过程。最后通过实际安装Windows Server 2003,以加深对该操作系统的理解并掌握
VMWare和Alcohol 120%等常用工具软件的使用。
本章书本上内容理论部分太深,涉及面太广,而且和实际工作联系不是很紧密,不建议
学生全文阅读。


智慧谷程序员俱乐部广聚各路好手!
www.zhihuigu.com
gks_cn 2005-04-12
  • 打赏
  • 举报
回复
我用的是hibernate3。0,数据库连接是用的jndi。服务器是webshpere
jianghuxing 2005-04-12
  • 打赏
  • 举报
回复
public static void initialize (String configFileName) throws HibernateException {
if (null == configFileName && sessionFactoryMap.size() > 0) return;
else if (null != sessionFactoryMap.get(configFileName)) return;
else {
Configuration cfg = new Configuration();
if (null == configFileName)
cfg.configure();
else
cfg.configure(configFileName);
setSessionFactory(configFileName, cfg.buildSessionFactory());
}
}

/**
* Set the session factory
*/
protected static void setSessionFactory (SessionFactory sessionFactory) {
setSessionFactory((String) null, sessionFactory);
}

/**
* Set the session factory
*/
protected static void setSessionFactory (String configFileName, SessionFactory sessionFactory) {
sessionFactoryMap.put(configFileName, sessionFactory);
}

/**
* Return the SessionFactory that is to be used by these DAOs. Change this
* and implement your own strategy if you, for example, want to pull the SessionFactory
* from the JNDI tree.
*/
protected SessionFactory getSessionFactory() throws HibernateException {
return getSessionFactory (getConfigurationFileName());
}

private static SessionFactory getSessionFactory(String configFile) throws HibernateException {
if (sessionFactoryMap.size() == 1) return (SessionFactory) sessionFactoryMap.values().toArray()[0];
else {
SessionFactory sessionFactory = (SessionFactory) sessionFactoryMap.get(configFile);
if (null == sessionFactory)
if (null == configFile)
throw new RuntimeException("The session factory has not been initialized.");
else
throw new RuntimeException("The session factory for '" + configFile + "' has not been initialized.");
else
return sessionFactory;
}
}

/**
* Return a new Session object that must be closed when the work has been completed.
* @return the active Session
*/
protected Session getSession() throws HibernateException {
return createSession();
}

/**
* Return a new Session object that must be closed when the work has been completed.
* @return the active Session
*/
public static Session createSession() throws HibernateException {
return createSession(null);
}

/**
* Return a new Session object that must be closed when the work has been completed.
* @param configFile the config file must match the meta attribute "config-file" in the hibernate mapping file
* @return the active Session
*/
public static Session createSession(String configFile) throws HibernateException {
java.util.Stack sessionStack = (java.util.Stack) threadedSessions.get();
Session session = null;
if (null == sessionStack) {
sessionStack = new java.util.Stack();
threadedSessions.set(sessionStack);
}
if (sessionStack.size() > 0) {
Object[] arr = (Object[]) sessionStack.peek();
String cf = (String) arr[0];
if (null == cf) {
session = (Session) arr[1];
}
else if (null != cf && null != configFile) {
if (cf.equals(configFile)) session = (Session) arr[1];
}
if (null == session) {
session = getSessionFactory(configFile).openSession();
arr = new Object[2];
arr[0] = configFile;
arr[1] = session;
sessionStack.push(arr);
}
}
else {
session = getSessionFactory(configFile).openSession();
Object[] arr = new Object[2];
arr = new Object[2];
arr[0] = configFile;
arr[1] = session;
sessionStack.push(arr);
}
return session;
}
jianghuxing 2005-04-12
  • 打赏
  • 举报
回复
<?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>
<!-- local connection properties -->
<property name="hibernate.connection.url">
jdbc:mysql://192.168.1.90/db_congress?useUnicode=true&characterEncoding=gbk
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password" />
<!-- property name="hibernate.connection.pool_size"></property -->
<!-- dialect for MySQL -->
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.use_outer_join">true</property>
<property name="hibernate.transaction.factory_class">
net.sf.hibernate.transaction.JDBCTransactionFactory
</property>
<mapping resource="com/netec/congress/model/TbSysVocation.hbm" />
<mapping resource="com/netec/congress/model/TbDeputyBaseinfo.hbm" />
<mapping resource="com/netec/congress/model/TbSysMeetingtype.hbm" />
<mapping resource="com/netec/congress/model/TbSysVotezone.hbm" />
<mapping resource="com/netec/congress/model/TbMeetingdoc.hbm" />
<mapping resource="com/netec/congress/model/TbSysClan.hbm" />
<mapping resource="com/netec/congress/model/TbSysDegree.hbm" />
<mapping resource="com/netec/congress/model/TbMeeting.hbm" />
<mapping resource="com/netec/congress/model/TbAgendaitem.hbm" />
<mapping resource="com/netec/congress/model/TbDeputation.hbm" />
<mapping resource="com/netec/congress/model/TbSysNation.hbm" />
<mapping resource="com/netec/congress/model/TbSysMeetingdoctype.hbm" />
<mapping resource="com/netec/congress/model/TbSysPeriod.hbm" />
<mapping resource="com/netec/congress/model/TbDeputyExtendinfo.hbm" />
<mapping resource="com/netec/congress/model/TbSysConfig.hbm" />
<mapping resource="com/netec/congress/model/TbAuditpersonInfo.hbm" />
<mapping resource="com/netec/congress/model/TbSysAuditsort.hbm" />
</session-factory>
</hibernate-configuration>
bestdelphier 2005-04-12
  • 打赏
  • 举报
回复
我也初学hibernate,但是我写的程序没有这个问题,up&mark。
gks_cn 2005-04-12
  • 打赏
  • 举报
回复
问题解决了。是因为2。0的dtd和3。0的包不匹配造成的。
gks_cn 2005-04-12
  • 打赏
  • 举报
回复
我用的hb3。0。但是配置
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
<session-factory name="foo">
<mapping resource="struts/advinfo/db/Advisor.hbm.xml"/>
<mapping resource="sys/bm/Duty.hbm.xml"/>
<mapping resource="struts/message/bo/Accessories.hbm.xml"/>
<mapping resource="struts/message/bo/Body.hbm.xml"/>
<mapping resource="struts/message/bo/Send.hbm.xml"/>
</session-factory>
</hibernate-configuration>
没有下到3。0的配置摸班
ChDw 2005-04-12
  • 打赏
  • 举报
回复
在运行的时候快吗?

还有就是你的XML中声明的DTD版本和hibernate的版本匹配吗?
gks_cn 2005-04-12
  • 打赏
  • 举报
回复
行,可是启动服务器的时候变慢了。
gks_cn 2005-04-12
  • 打赏
  • 举报
回复
这个不能治本。主要是调试的时候暴慢。受不了了。
jianghuxing 2005-04-12
  • 打赏
  • 举报
回复
public class IntializtionServlet extends HttpServlet {
public void init() throws ServletException {
try {
..........................
factory = new Configuration().configure().buildSessionFactory();
System.out.println("数据库初始化成功");
} catch (HibernateException e) {
e.printStackTrace();
System.out.println("数据库初始化失败");
}
}

然后在web.xml中加:
<servlet>
<servlet-name>CongressConfigInit</servlet-name>
<servlet-class>
XXX.IntializtionServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>


gks_cn 2005-04-12
  • 打赏
  • 举报
回复
如何写?有没有相关资料和例子?
dkmilk 2005-04-12
  • 打赏
  • 举报
回复
初始化很慢是正常现像,可以写成plug,在服务器起动时初始化,
加载更多回复(1)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧