﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-thebigcrow-文章分类-Hibernate</title><link>http://www.blogjava.net/thebigcrow/category/4286.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 03:57:34 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 03:57:34 GMT</pubDate><ttl>60</ttl><item><title>在Weblogic上配置Hibernate为JNDI [网络转帖，作者不详]</title><link>http://www.blogjava.net/thebigcrow/articles/17220.html</link><dc:creator>技术乌鸦</dc:creator><author>技术乌鸦</author><pubDate>Fri, 28 Oct 2005 09:54:00 GMT</pubDate><guid>http://www.blogjava.net/thebigcrow/articles/17220.html</guid><wfw:comment>http://www.blogjava.net/thebigcrow/comments/17220.html</wfw:comment><comments>http://www.blogjava.net/thebigcrow/articles/17220.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/thebigcrow/comments/commentRss/17220.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/thebigcrow/services/trackbacks/17220.html</trackback:ping><description><![CDATA[一、首先需要把Hibernate 用到的jar包和配置文件都放到Weblogic能够搜索到的CLASSPATH路径上。单单这一步就有很多人很迷茫，其实去仔细看看Weblogic的启动脚本文件startWeblogic.cmd和startWLS.cmd，我想大部分人都知道该怎么配置了。<BR><BR>我机器上的有个Hibernate的项目，在D:\test\oracle目录下，该目录下的结构是：<BR><BR>D:\test\oracle\lib 放置hibernate的所有jar包<BR>D:\test\oracle\src 放置源代码<BR>D:\test\oracle\classes 编译好的代码和hibernate的配置文件(hibernate.properties, log4j.properties, cache.ccf)<BR><BR>现在需要把D:\test\oracle\lib目录下那些jar文件和D:\test\oracle\classes目录都放置到Weblogic的 CLASSPATH里面去，所以修改mydomain里面的Weblogic启动脚本startWeblogic.cmd，在启动Weblogic之前，插入设置CLASSPATH的命令，如下：<BR><BR><PRE class=overflow title="pre code">@rem set hibernate classpath<BR>set HIBERNATE_LIB=D:\test\oracle\lib<BR>set HIBERNATE_CLASSES=D:\test\oracle\classes<BR>set CLASSPATH=%HIBERNATE_LIB%\cglib-asm.jar;%HIBERNATE_LIB%\commons-beanutils.jar;<BR>%HIBERNATE_LIB%\commons-collections.jar;%HIBERNATE_LIB%\commons-lang.jar;<BR>%HIBERNATE_LIB%\commons-logging.jar;%HIBERNATE_LIB%\dom4j-full.jar;<BR>%HIBERNATE_LIB%\hibernate2.jar;%HIBERNATE_LIB%\jcs.jar;<BR>%HIBERNATE_LIB%\log4j-1.2.8.jar;%HIBERNATE_LIB%\odmg.jar;<BR>%HIBERNATE_CLASSES%;%CLASSPATH%</PRE><BR><BR>下面一行，就是本来脚本里面的启动命令：<BR><BR><PRE class=overflow title="pre code">@rem Call Weblogic Server<BR>call "C:\bea\weblogic700\server\bin\startWLS.cmd"</PRE><BR><BR>二、在Weblogic上配置 Oracle数据库的连接池。这一步本来和Hibernate无关，但是如果你想要使用EJB，想要使用JTA，那么必须使用Weblogic提供的连接池，而不能使用Hibernate自带的连接池，或者其它第三方连接池，否则容器将无法管理数据库事务。这一步很简单，就是在Weblogic Console里面配置Connection Pool和TxData Source，我的TxDataSource取名称为“mypool”<BR><BR>三、修改hibernate.properties。使用Weblogic的连接池，而不是自带的连接池。我修改的是D:\test\oracle\classes\hibernate.properties，增加如下行：<BR><BR><PRE class=overflow title="pre code">hibernate.dialect net.sf.hibernate.dialect.OracleDialect<BR>hibernate.connection.datasource mypool<BR>hibernate.connection.provider_class net.sf.hibernate.connection.DatasourceConnectionProvider<BR>hibernate.session_factory_name hibernate.session_factory</PRE><BR><BR>注意最后一行，这是使用 Hibernate来绑定JNDI给JNDI起的名称，本来应该是hibernate/session_factory，但是Weblogic要求改为. 号，不过在程序中lookup的时候还是要写hibernate/session_factory<BR><BR>另外提到一点的是<BR><BR>hibernate.jdbc.fetch_size 50<BR>hibernate.jdbc.batch_size 25<BR><BR>分别对数据库查询和插入有很大的性能影响，调节这两个选项可以得到最好的性能。<BR><BR>为了保证SessionFactory实例的预创建，使用Weblogic的T3StartUpDef接口创建一个StartUp类，在Weblogic启动的时候运行：<BR><BR><PRE class=overflow title="pre code">package com.fankai;<BR><BR>import java.util.Hashtable;<BR>import weblogic.common.T3StartupDef;<BR>import weblogic.common.T3ServicesDef;<BR>import net.sf.hibernate.cfg.Configuration;<BR>import net.sf.hibernate.SessionFactory;<BR><BR>public class HibernateStartUp implements T3StartupDef {<BR><BR>public void setServices(T3ServicesDef services) {}<BR><BR>public String startup(String name, Hashtable args) throws Exception {<BR>Configuration conf = new Configuration().addClass(Cat.class);<BR>SessionFactory sf = conf.buildSessionFactory(); <BR>return "Hibernate Startup completed successfully";<BR>} <BR>}</PRE><BR><BR>代码非常简单，其实就是确保预先运行<BR><BR><PRE class=overflow title="pre code">Configuration conf = new Configuration().addClass(Cat.class);<BR>SessionFactory sf = conf.buildSessionFactory(); </PRE><BR><BR>把sf创建出来，而Hibernate会自行调用一系列类方法，把sf绑定到Weblogic的的JNDI树下的hibernate/session_factory路径中。<BR><BR>4、编译HibernateStartUp.java<BR><BR>编译这个源代码的时候需要注意的是，要把weblogic.jar包和Hibernate所有的相关包和配置文件导入。我是把这个源代码放到D:\test\oracle\src目录下的，用早已编写好的ant脚本运行一下就编译好了，并且编译好的 class文件被放置到D:\test\oracle\classes目录下，该目录已经被加入到Weblogic的CLASSPATH里面，因此很省事。<BR><BR>五、配置StartUp类<BR><BR>启动Weblogic，打开Console控制台，在左边的Applet树上找到StartUp & Shutdown，然后在右边点击“Configure a new Startup Class...”，在Name框里面随便填写，在ClassName里面填写你编写的StartUp类，我填写的是 com.fankai.WLSStartup，然后点击“Apply”。然后切换到Target这选项卡，在Target-Server左边的 Avaiable框里面选择“myserver”，点击右箭头，把它挪到右边的“Chosen”框里面去，最后再点击一下“Apply"按钮。如果此时 Weblogic的DOS窗口里面没有出错信息，那么应该已经配置成功了。<BR><BR>六、现在关闭Weblogic，再重新运行 startWelogic.cmd，启动Weblogic，观察DOS窗口的输出信息，可以看到Hibernate的初始化信息一屏屏的滚动输出，证明已经配置成功。现在再打开Console控制台，点击左边Applet树中的Servers|myserver，然后可以在右边最下面找到“View JNDI tree ”，点击它，会打开一个浏览器窗口，显示JNDI树，这时你可以看到一个名称为hibernate的JNDI对象，在左边的Applet树中点击它，看右边的详细信息，我的机器上的信息如下：<BR><BR><PRE class=overflow title="pre code">Bind Name: hibernate<BR>Object Class: net.sf.hibernate.impl.SessionFactoryImpl<BR>Object Hash Code: 454492<BR>Object To String: net.sf.hibernate.impl.SessionFactoryImpl@6ef5c</PRE><BR><BR>完全正确！<BR><BR>最后你可以随意在EJB或者Servlet/JSP里面使用JND查找来获得SessionFactory了。<BR><BR>例如：<BR><BR><PRE class=overflow title="pre code">Context ctx = new InitialContext();<BR>SessionFactory sf = (SessionFactory) ctx.lookup("hibernate/session_factory");</PRE><BR><BR>在其它App Server上如何配置，本质上和Weblogic是一样的，只是你要确保在其它程序运行之前，先把<BR><BR><PRE class=overflow title="pre code">Configuration conf = new Configuration().addClass(Cat.class);<BR>SessionFactory sf = conf.buildSessionFactory(); </PRE><BR><BR>这两行代码运行一遍就OK了<img src ="http://www.blogjava.net/thebigcrow/aggbug/17220.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/thebigcrow/" target="_blank">技术乌鸦</a> 2005-10-28 17:54 <a href="http://www.blogjava.net/thebigcrow/articles/17220.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>