[本站首页] [管理页面] [写新日志] [退出]

朝为田舍郎,暮登天子堂,将相本无种,男儿当自强。
首页(178) Hibernate(10) JAVA(19) Web(15) Struts(7) 口水(9) Ides(18) 其它(51) AJAX(6) database(29) 
Blog信息

blog名称:四裤全输的小窝~~
日志总数:178
评论数量:699
留言数量:198
访问次数:1152417
建立时间:2005年10月29日

Blog内搜索



日志更新

谷歌地图定位偏移解决方法
【转】利用Windows内置的命令作端口
WIN2003服务器安全加固方案
[转]sql server 日期比较、日
MediaCoder 一般参数设置
[转]VMware中创建共享磁盘阵列的方
缓解vss共享文件夹的安全隐患问题(转)
Delphi 中调用JavaScript
微软的官方方法:延长Windows Se
cxGrid 过滤 排序后 取选中记录的

最新评论

回复:谷歌地图定位偏移解决方法
回复:谷歌地图定位偏移解决方法
回复:WIN2003服务器安全加固方案
回复:cxGrid 过滤 排序后 取选中
回复:TreeView 父节点 子节点 
回复:[转]Oracle Instead
回复:DWR 官方下载地址
ugg  boots
回复:cxGrid 过滤 排序后 取选中
回复:DWR 官方下载地址

友情链接

biglin's Blog
NoisyRam's Blog
Stone's Blog
Kevin 的小家
『知』治通鉴

留言板




[Hibernate]weblogic中将SessionFactory配置为JNDI
文章收藏,  网上资源,  软件技术,  电脑与网络 四裤全输 发表于 2006/7/5 10:44:46

转自:http://searchwebservices.techtarget.com.cn/tips/315/2175315.shtml为了更方便的在编写程序的过程中运用hibernate并且在多个应用程序中共享SessionFactory,将生成的SessionFactory配置为jndi并且在服务器启动的时候将其配置为自动运行是个不错的方法,这样在程序中就可以通过“查找”的方式直接获取factory的引用,步骤如下:  1. 如果单单是为了将SessionFactory配置为JNDI,方法很简单,只要在hibernate.cfg.xml文件中配置一下要绑定到jndi的名字即可,格式如下:<hibernate-configuration>  <session-factory name="hibernate.session_factory">  <property name="connection.datasource">jdbc.name</property>  ……………………  /session-factory>  </hibernate-configuration>   其中name="hibernate.session_factory"指定要绑定的JNDI名字为 hibernate/session_factory,这样在hibernate初始化SessionFactory的时候就会将其绑定在jndi服务上,之后我们就可以在程序中通过JNDI查找来获取SessionFactory的引用,如:  Context context=new InitialContext();  SessionFactory sf = (SessionFactory)context.lookup("hibernate/session_factory");  2. 上述中通过JNDI查找SessionFactory的引用的前提是在查找之前必须已经绑定,也就是hibernate已经初始化过 SessionFactory,为了保证此条件,比较好的方法就是在服务器启动的时候自动运行初始化SessionFactory的代码并将其绑定至 JNDI,可以通过以下两种方式实现服务器启动的时候自动运行代码:  对于weblogic来说,我们可以通过weblogic.common.T3StartupDef接口来实现一个StartUp类,然后在weblogic控制台中将其配置为自动运行,步骤如下:  1) 编写StartUp类: package com.starup  public class HibernateInitStartUp implements   weblogic.common.T3StartupDef{  public void setServices(T3ServicesDef t3ServicesDef) {  }  public String startup(String string, Hashtable hashtable) throws Exception {  Configuration conf = new Configuration().configure();  SessionFactory sf = conf.buildSessionFactory();  return "SessionFactory init complete and bound to JNDI successful";  }  }   2) 编译该类  3) 修改startWebLogic.cmd文件,将上述类中用到的类文件和hibernate关联的包增加到其启动时的classpath环境变量中,在加入的时候要根据hibernate的版本而定,例如我用的是2.0,如果其他的版本上述的文件名可能有些不同,此时一定要检查好  4) 打开weblogic控制台,在左侧的树中点击StartUp &amp; Shutdown,然后在右边的主页面中点击“Configure a new Startup Class...”配置一个自动运行类,在Name框里面随便填写,在ClassName里面填写你编写的StartUp类:com.starup. HibernateInitStartUp,然后点击apply,在接下来的Target选项中选择myserver(根据你的情况而定),点击 apply按钮。  5) 重新启动weblogic,可以看到hibernate初始化的一些信息和提示将SessionFactory绑定至JNDI的提示信息  6) 在weblogic控制台中的jndi树中可以看到绑定的情况(点击mydomain—server—myserver,然后点击右边页面最下方的“View JNDI tree”就可以看到其信息)  通过实现ServletContextListener接口在系统初始化一个应用程序的时候自动运行,步骤如下:  1) 编写类,实现ServletContextListener接口,如下:  package com.starup;    import net.sf.hibernate.SessionFactory;  import net.sf.hibernate.cfg.Configuration;    import javax.servlet.ServletContextEvent;  import javax.servlet.ServletContextListener;  public class HibernateInitStartUp implements ServletContextListener {  public void contextDestroyed(ServletContextEvent servletContextEvent) {    }    public void contextInitialized(ServletContextEvent servletContextEvent) {    try {  Configuration conf = new Configuration().configure();  SessionFactory sf = conf.buildSessionFactory();  } catch (Exception e) {  e.printStackTrace();  }  }  }   2) 在对应应用程序的web.xml文件中加入:  com.starup. HibernateInitStartUp  3) 启动wls,系统会在初始化对应应用程序的时候自动运行。  此方法不用配置weblogic的启动环境变量,因为当初始化应用程序的时候,对应的类和包已经加载到环境变量中。


阅读全文(2453) | 回复(0) | 编辑 | 精华


发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.063 second(s), page refreshed 144769474 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号