2009-12-21 62 views
0

我正在尝试运行休眠碎片的示例程序。 我都用它做,但每当我运行测试程序我得到一个异常休眠碎片和JNDI问题

javax.naming.NoInitialContextException: Need to specify class name in environment or system property

google搜索我才知道,我已经设置JNDI属性后。 。我不喜欢这样

Hashtable env = new Hashtable(); 
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.enterprise.naming.SerialInitContextFactory"); 
env.put(Context.PROVIDER_URL, "<some-IP>:3306"); 
Context initialContext = new InitialContext(env); 

javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory] 
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657) 
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) 
at javax.naming.InitialContext.init(InitialContext.java:223) 
at javax.naming.InitialContext.<init>(InitialContext.java:197) 
at com.hibshards.test.JNDIProperties.setProperties(JNDIProperties.java:18) 
at com.hibshards.test.SessionFactoryImpl.createSessionFactory(SessionFactoryImpl.java:32) 
at com.hibshards.test.ShardTest.main(ShardTest.java:17) 
Caused by: java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory 
at java.net.URLClassLoader$1.run(URLClassLoader.java:200) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.net.URLClassLoader.findClass(URLClassLoader.java:188) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:307) 
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:252) 
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) 
at java.lang.Class.forName0(Native Method) 
at java.lang.Class.forName(Class.java:247) 
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:46) 
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654) 
... 6 more 

但仍东西不工作:-( 请帮什么n,其中我失踪

下面是代码:

public class SessionFactoryImpl { 

    private static final String SHARD_CFG_0 = "/com/hibshards/config/shard0.hibernate.cfg.xml"; 
    private static final String SHARD_CFG_1 = "/com/hibshards/config/shard1.hibernate.cfg.xml"; 
    private static final String SHARDED_TABLE = "com/hibshards/orm/weather.hbm.xml"; 

    public static SessionFactory createSessionFactory() throws NamingException { 
     Configuration prototypeConfig = new Configuration().configure(SHARD_CFG_0); 
     prototypeConfig.addResource(SHARDED_TABLE); 

     List<ShardConfiguration> shardConfigs = new ArrayList<ShardConfiguration>(); 
     shardConfigs.add(buildShardConfig(SHARD_CFG_0)); 
     shardConfigs.add(buildShardConfig(SHARD_CFG_1)); 

     ShardStrategyFactory shardStrategyFactory = buildShardStrategyFactory(); 
     ShardedConfiguration shardedConfig = new ShardedConfiguration(
       prototypeConfig, 
       shardConfigs, 
       shardStrategyFactory); 
     return shardedConfig.buildShardedSessionFactory(); 
    } 

    private static ShardStrategyFactory buildShardStrategyFactory() { 

     ShardStrategyFactory shardStrategyFactory = new ShardStrategyFactory() { 
      public ShardStrategy newShardStrategy(List<ShardId> shardIds) { 
       RoundRobinShardLoadBalancer loadBalancer = new RoundRobinShardLoadBalancer(shardIds); 
       ShardSelectionStrategy pss = new RoundRobinShardSelectionStrategy(loadBalancer); 
       ShardResolutionStrategy prs = new AllShardsShardResolutionStrategy(shardIds); 
       ShardAccessStrategy pas = new SequentialShardAccessStrategy(); 
       return new ShardStrategyImpl(pss, prs, pas); 
      } 
     }; 
     return shardStrategyFactory; 
    } 

    private static ShardConfiguration buildShardConfig(String configFile) { 
     Configuration config = new Configuration().configure(configFile); 
     return new ConfigurationToShardConfigurationAdapter(config); 
    } 
} 

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD//EN" 
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

    <hibernate-configuration> 
    <session-factory name="HibernateSessionFactory0"> 
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="connection.url">jdbc:mysql://localhost:3306/shardDB</property> 
    <property name="connection.username">root</property> 
    <property name="connection.password">root</property> 
    <property name="hibernate.connection.shard_id">0</property> 
    <property name="hibernate.shard.enable_cross_shard_relationship_checks">true</property> 
    </session-factory> 
    </hibernate-configuration> 


public class JNDIProperties { 
public static void setProperties() throws NamingException { 
    Properties properties = new Properties(); 
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory"); 
    properties.put(Context.PROVIDER_URL, "file:///"); 
    Context initialContext = new InitialContext(properties); 
    } 
      } 
+0

JDNI环境设置正确(provi der URL似乎在使用一个非常奇怪的mysql端口)。请提供有关您的代码正在执行的更多详细信息。你想要查找什么?你在使用什么应用服务器?还请添加完整的堆栈跟踪。 – 2009-12-21 06:33:34

+0

我试图创建一个分片环境。一种演示应用程序。 它不使用任何服务器。我的数据库驻留在两台不同的机器上,所以我通过了该机器的IP地址。 javax.naming.NoInitialContextException:需要在环境或系统属性中,或作为applet参数或应用程序资源文件中指定类名称:java.naming.factory.initial \t at javax.naming.spi.NamingManager.getInitialContext (NamingManager.java:645) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) – Rites 2009-12-21 06:48:01

+0

请编辑您的问题以添加** full ** stacktrace。 – 2009-12-21 07:00:37

回答

2

在一个简单的Java应用程序,你不需要会话工厂的名称..try删除你的cfg.xml文件中的名称并执行它