Je suis en train d'essayer d'exécuter un programme d'exemple pour hibernate shards. J'ai tout terminé, mais à chaque fois que je lance le programme de test, j'obtiens une Exception
javax.naming.NoInitialContextException: Need to specify class name in environment or system property
Après avoir fait des recherches sur Google, j'ai découvert que je dois définir les propriétés JNDI. J'ai fait comme ceci
Hashtable env = new Hashtable();
env.put( Context.INITIAL_CONTEXT_FACTORY,"com.sun.enterprise.naming.SerialInitContextFactory");
env.put( Context.PROVIDER_URL, ":3306" );
Context initialContext = new InitialContext(env);
javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory [L'exception principale est 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.(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
Mais les choses ne fonctionnent toujours pas. :-( Merci de m'aider. Qu'est-ce que je manque et où ?
Voici le 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 shardConfigs = new ArrayList();
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 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 );
}
}
org.hibernate.dialect.MySQLDialect
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/shardDB
root
root
0
true
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 );
}
}