English 中文(简体)
Problem with Hibernate Shards & JNDI
原标题:

I m trying to run a sample program for hibernate shards. I m all done with it but whenever I run the test program I get an Exception

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

After googling I got to know that I ve to set the JNDI properties. I did like this

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

But still things are not working.:-( Please help. What n where I m missing?

Here is the code :

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 );
  }
          }
问题回答

In a simple java application, you don t need a name for the session factory..try removing the name in your cfg.xml file and executing it





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签