论坛首页 Java版 Hibernate

『出错』我在Weblogic8.1下配置Hiberante为JNDI出现的问题

浏览 4384 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2004-10-12
软件环境:
WebLogic8.1
Hbiernate2.1.2[/i]
配置文件:
hibernate.properties如下:
hibernate.dialect net.sf.hibernate.dialect.Oracle9Dialect
hibernate.connection.datasource MyTestDataSource
hibernate.connection.provider_class net.sf.hibernate.connection.DatasourceConnectionProvider
hibernate.session_factory_name hibernate.session_factory
hibernate.connection.pool_size 10
hibernate.jdbc.fetch_size 50
hibernate.jdbc.batch_size 25

Test文件源代码:

   Properties containerEnv = new Properties();
    containerEnv.setProperty("java.naming.factory.initial","weblogic.jndi.WLInitialContextFactory");
    containerEnv.setProperty("java.naming.provider.url","t3://localhost:7001");
    InitialContext   ctx = new InitialContext(containerEnv);
    try {
      sessionFactory = (SessionFactory) ctx.lookup("hibernate/session_factory");
      if(sessionFactory == null){
      System.out.println("取到空值");}
    }
    catch (NamingException ex) {
      System.out.print(ex.toString());
    }




错误提示信息:
Weblogic输出如下:其中的中文是我在SartUp文件中打印的。

&lt;2004-10-12 下午17时25分15秒 CST&gt; &lt;Notice&gt; &lt;WebLogicServer&gt; <BEA-000327> &lt;Starti
ng WebLogic Admin Server "myserver" for domain "mydomain"&gt;
17:25:53,613  INFO Environment:462 - Hibernate 2.1.2
17:25:53,692  INFO Environment:496 - loaded properties from resource hibernate.p
roperties: {hibernate.jdbc.fetch_size=50, hibernate.cglib.use_reflection_optimiz
er=true, hibernate.dialect=net.sf.hibernate.dialect.Oracle9Dialect, hibernate.jd
bc.batch_size=25, hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N'
, hibernate.session_factory_name=hibernate.session_factory, hibernate.connection
.datasource=MyTestDataSource, hibernate.connection.provider_class=net.sf.hiberna
te.connection.DatasourceConnectionProvider, hibernate.connection.pool_size=10}
17:25:53,707  INFO Environment:519 - using CGLIB reflection optimizer
17:25:53,723  INFO Configuration:329 - Mapping resource: persistobj/Corp.hbm.xml

17:25:54,707  INFO Binder:229 - Mapping class: persistobj.Corp -&gt; TBL_CORP
17:25:54,910  INFO Configuration:329 - Mapping resource: persistobj/Category.hbm
.xml
17:25:55,004  INFO Binder:229 - Mapping class: persistobj.Category -&gt; TBL_CATEGO
RY
17:25:55,020  INFO Configuration:329 - Mapping resource: persistobj/Product.hbm.
xml
17:25:55,223  INFO Binder:229 - Mapping class: persistobj.Product -&gt; TBL_PRODUCT

17:25:55,238  INFO Configuration:595 - processing one-to-many association mappin
gs
17:25:55,238  INFO Configuration:604 - processing one-to-one association propert
y references
17:25:55,238  INFO Configuration:629 - processing foreign key constraints
17:25:55,285  INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.Oracle9D
ialect
17:25:55,301  INFO SettingsFactory:55 - JDBC result set fetch size: 50
17:25:55,301  INFO SettingsFactory:62 - Use outer join fetching: true
17:25:55,317  INFO ConnectionProviderFactory:53 - Initializing connection provid
er: net.sf.hibernate.connection.DatasourceConnectionProvider
17:25:55,317  INFO NamingHelper:26 - JNDI InitialContext properties:{}
17:25:55,332  INFO DatasourceConnectionProvider:51 - Using datasource: MyTestDat
aSource
17:25:55,348  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLoo
kup configured (in JTA environment, use of process level read-write cache is not
recommended)
17:25:55,988  INFO SettingsFactory:102 - Use scrollable result sets: true
17:25:55,988  INFO SettingsFactory:105 - Use JDBC3 getGeneratedKeys(): false
17:25:55,988  INFO SettingsFactory:108 - Optimize cache for minimal puts: false
17:25:55,988  INFO SettingsFactory:117 - Query language substitutions: {no='N',
true=1, yes='Y', false=0}
17:25:55,988  INFO SettingsFactory:128 - cache provider: net.sf.ehcache.hibernat
e.Provider
17:25:56,004  INFO Configuration:1080 - instantiating and configuring caches
17:25:56,910  INFO SessionFactoryImpl:119 - building session factory
17:25:59,582  INFO SessionFactoryObjectFactory:86 - Factory name: hibernate.sess
ion_factory
17:25:59,582  INFO NamingHelper:26 - JNDI InitialContext properties:{}
17:25:59,676  INFO NamingHelper:68 - Creating subcontext: hibernate
17:25:59,692  INFO SessionFactoryObjectFactory:91 - Bound factory to JNDI name:
hibernate.session_factory
17:25:59,707  WARN SessionFactoryObjectFactory:101 - InitialContext did not impl
ement EventContext
此处有执行HibernateStartUP文件
&lt;2004-10-12 下午17时26分00秒 CST&gt; &lt;Notice&gt; &lt;WebLogicServer&gt; <BEA-000331> &lt;Starte
d WebLogic Admin Server "myserver" for domain "mydomain" running in Development
Mode&gt;
&lt;2004-10-12 下午17时26分00秒 CST&gt; &lt;Notice&gt; &lt;WebLogicServer&gt; <BEA-000360> &lt;Server
started in RUNNING mode&gt;
&lt;2004-10-12 下午17时26分00秒 CST&gt; &lt;Notice&gt; &lt;WebLogicServer&gt; <BEA-000355> &lt;Thread
"ListenThread.Default" listening on port 7001, ip address *.*&gt;

JNDI TREE中显示的信息如下:
   Bind Name: session_factory
   Object Class: net.sf.hibernate.impl.SessionFactoryImpl
   Object Hash Code: 19006837
   Object To String: net.sf.hibernate.impl.SessionFactoryImpl@1220575


我的test文件出错信息如下:

java.lang.NullPointerException

at persistobj.Test2.main(Test2.java:34)

Exception in thread "main" 17:29:37,645  WARN SessionFactoryObjectFactory:148 - Not found: 2c9a8397ff8c754400ff8c75528f0000

取到空值


你的分析:
看Weblogic的输出证明自己写的初始文件已经生效,且在Weblogic的JNDI树中也可以看见已经绑定的sessionFactory,但为什么我在ctx.lookup的时候还是找回空值呢,启动时的如下警告信息是否有问题?:
17:25:59,707  WARN SessionFactoryObjectFactory:101 - InitialContext did not impl
ement EventContext

谁能帮忙解决一下吗,非常感谢!!
   
最后更新时间:2004-10-12
找了论坛上的一些文章,这篇里面描述的问题和我遇到的完全一样

http://forum.javaeye.com/viewtopic.php?t=1806&highlight=jndi+weblogic

但是文章最后没有给出解决问题的方案,希望找到问题原因的朋友指点一二,谢谢了
   
0 请登录后投票
最后更新时间:2004-10-12
我刚刚又尝试了一遍你的代码,明白是怎么回事了。

[code:1]public final class SessionFactoryImpl implements SessionFactory, SessionFactoryImplementor[/code:1]

上面是SessionFactory源代码定义,SessionFactory并没有实现序列化接口,因此当你的客户端程序(在另一个单独的JVM中运行)远程访问WebLogic JNDI,企图将SessionFactory序列化到本地,肯定会失败。

但即使SessionFactory实现序列化接口,由于它不是一个可以支持RMI的对象,就算你可以lookup到该对象,仍然无法在WebLogic容器外正常调用。

与此不同的是,WebLogic本身的DataSource,EJB,JMS等等都是支持RMI的(前提条件是WebLogic相应的jar要有),所以你可以在WebLogic外面lookup,并且使用它。

请仔细阅读我那篇文章,我强调的是这个绑定到JNDI上的SessionFactory可以被该WebLogic里面的EJB/JSP调用,但是并没有说可以被WebLogic以外的客户端程序调用。
   
0 请登录后投票
最后更新时间:2004-10-12
非常感谢,这个原因开始我也有怀疑,然后写了一个EJB发布后进行调用,程序没有报错但结果仍不正确,可能是我的EJB有其他问题,我会再仔细检查一下

刚刚阅读完《提问智慧》和《删贴规则》,发现挺严厉,但确实有道理,呵呵,再次感谢robbin
   
0 请登录后投票
最后更新时间:2004-10-14
robbin 写道
我刚刚又尝试了一遍你的代码,明白是怎么回事了。

[code:1]public final class SessionFactoryImpl implements SessionFactory, SessionFactoryImplementor[/code:1]

上面是SessionFactory源代码定义,SessionFactory并没有实现序列化接口,因此当你的客户端程序(在另一个单独的JVM中运行)远程访问WebLogic JNDI,企图将SessionFactory序列化到本地,肯定会失败。

但即使SessionFactory实现序列化接口,由于它不是一个可以支持RMI的对象,就算你可以lookup到该对象,仍然无法在WebLogic容器外正常调用。

与此不同的是,WebLogic本身的DataSource,EJB,JMS等等都是支持RMI的(前提条件是WebLogic相应的jar要有),所以你可以在WebLogic外面lookup,并且使用它。

请仔细阅读我那篇文章,我强调的是这个绑定到JNDI上的SessionFactory可以被该WebLogic里面的EJB/JSP调用,但是并没有说可以被WebLogic以外的客户端程序调用。


可以把SessionFactory封装到一个类里,然后让这个类implement rmi相关的接口来实现吗?我最近想把proxool的连接池绑定到weblogic的jndi上的时候也碰到类似的问题,在jvm外不能正常使用
   
0 请登录后投票
最后更新时间:2004-10-14
zzeric 写道
robbin 写道
我刚刚又尝试了一遍你的代码,明白是怎么回事了。

[code:1]public final class SessionFactoryImpl implements SessionFactory, SessionFactoryImplementor[/code:1]

上面是SessionFactory源代码定义,SessionFactory并没有实现序列化接口,因此当你的客户端程序(在另一个单独的JVM中运行)远程访问WebLogic JNDI,企图将SessionFactory序列化到本地,肯定会失败。

但即使SessionFactory实现序列化接口,由于它不是一个可以支持RMI的对象,就算你可以lookup到该对象,仍然无法在WebLogic容器外正常调用。

与此不同的是,WebLogic本身的DataSource,EJB,JMS等等都是支持RMI的(前提条件是WebLogic相应的jar要有),所以你可以在WebLogic外面lookup,并且使用它。

请仔细阅读我那篇文章,我强调的是这个绑定到JNDI上的SessionFactory可以被该WebLogic里面的EJB/JSP调用,但是并没有说可以被WebLogic以外的客户端程序调用。


可以把SessionFactory封装到一个类里,然后让这个类implement rmi相关的接口来实现吗?我最近想把proxool的连接池绑定到weblogic的jndi上的时候也碰到类似的问题,在jvm外不能正常使用


那你要调用WebLogic的RMI实现,写很复杂的代码了。
   
0 请登录后投票
最后更新时间:2004-10-15
之前的问题正如robbin所说,用EJB查找JNDI后,问题都解决了。

但新的问题是,我同在
http://forum.javaeye.com/viewtopic.php?t=245&highlight=weblogic+jndi
文章中描述的一样,把classes目录添加到weblogic的启动classpath下,这方便了startup文件的正常运行,但是当新的模块部署时,例如EJB(该EJB类文件也在classes目录下,因为在JBX的同一个工程里),weblogic会从这个目录直接装载类文件,然后出现如下警告:
&lt;2004-10-15 上午09时55分36秒 CST&gt; &lt;Warning&gt; &lt;EJB&gt; <BEA-010001> &lt;While deploying EJB 'HibernateEJBTest', class ejb.HibernateEJBTestBean was loaded from the system classpath. As a result, this class cannot be reloaded while the server is running. To prevent this behavior in the future, make sure the class is not located in the server classpath.&gt;
&lt;2004-10-15 上午09时55分36秒 CST&gt; &lt;Warning&gt; &lt;EJB&gt; <BEA-010001> &lt;While deploying EJB 'HibernateEJBTest', class ejb.HibernateEJBTestHome was loaded from the system classpath. As a result, this class cannot be reloaded while the server is running. To prevent this behavior in the future, make sure the class is not located in the server classpath.&gt;
&lt;2004-10-15 上午09时55分36秒 CST&gt; &lt;Warning&gt; &lt;EJB&gt; <BEA-010001> &lt;While deploying EJB 'HibernateEJBTest', class ejb.HibernateEJBTest was loaded from the system classpath. As a result, this class cannot be reloaded while the server is running. To prevent this behavior in the future, make sure the class is not located in the server classpath.&gt;
之后,EJB的调试和重新部署就非常麻烦了,因为已经不能动态部署了,而且最终做发布的时候这样直接从从classes目录里装载应该也不行把?
我是不是应该把startup类同其他用到的类另外做一个工程然后同Weblogic绑定,与其他模块的开发分开?请问是否有更好的解决办法?
   
0 请登录后投票
最后更新时间:2004-11-03
分开打包,部署只需要部署的内容,防灾Classpath里面的不要包含部署的class
   
0 请登录后投票
最后更新时间:2004-11-04
public interface SessionFactory extends Referenceable, Serializable
但是没有 extends Remote,我在尝试使用另外一个进程lookup时也遇到同样问题
   
0 请登录后投票
最后更新时间:2004-11-05
mikecool 写道
分开打包,部署只需要部署的内容,防灾Classpath里面的不要包含部署的class


是这样的,我现在的做法是对StartUp类及其所需的持久类、hbm文件打成一个start.jar,放到WLS的classpath里面,启动及创建SessionFactory,部署EJB都完全正常了。
   
0 请登录后投票
论坛首页 Java版 Hibernate

跳转论坛:
JavaEye推荐