I am trying to access DB by hibernate through JBoss DataSource in order to get benefit of connection pooling. But encounter problem now.
When I lookup SessionFactory from JBoss server, through my java code like this:
Code:
public String getCompany(Integer custno) {
String company = "";
try {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
InitialContext ctx = new InitialContext(env);
SessionFactory sessionFactory = (SessionFactory)ctx.lookup("java:/HibernateFactory"); // Error here.
Session session = sessionFactory.getCurrentSession();
Customer customer = (Customer) session.get(Customer.class, custno);
if (customer != null) {
company = customer.getCompany();
}
} catch (NamingException e) {
e.printStackTrace();
}
return company;
}
The error messages are:
Code:
javax.naming.NameNotFoundException: HibernateFactory not bound
at org.jnp.server.NamingServer.getBinding(NamingServer.java:491)
at org.jnp.server.NamingServer.getBinding(NamingServer.java:499)
at org.jnp.server.NamingServer.getObject(NamingServer.java:505)
at org.jnp.server.NamingServer.lookup(NamingServer.java:278)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
at sun.rmi.transport.Transport$1.run(Transport.java:153)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
at java.lang.Thread.run(Thread.java:595)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:610)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:572)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.pkg1.ejb.CustomerBean.getCompany(CustomerBean.java:77)
at com.pkg1.client.ConsoleDemo.main(ConsoleDemo.java:18)
While application console told me that HibernateFactory not bound, JBoss console told me that HibernateFactory is bound indeed when it starts up.
Code:
JBoss startup console:--
...
19:23:27,264 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup:
org.hibernate.transaction.JBossTransactionManagerLookup
19:23:27,264 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
19:23:27,279 INFO [SettingsFactory] Automatic flush during beforeCompletion():disabled
19:23:27,279 INFO [SettingsFactory] Automatic session close at end of transaction: disabled
19:23:27,279 INFO [SettingsFactory] Second-level cache: enabled
19:23:27,279 INFO [SettingsFactory] Query cache: disabled
19:23:27,279 INFO [SettingsFactory] Cache provider: org.hibernate.cache.EhCacheProvider
19:23:27,326 INFO [SettingsFactory] Optimize cache for minimal puts: disabled
19:23:27,326 INFO [SettingsFactory] Structured second-level cache entries: enabled
19:23:27,342 INFO [SettingsFactory] Statistics: disabled
19:23:27,342 INFO [SettingsFactory] Deleted entity synthetic identifier rollback: disabled
19:23:27,357 INFO [SettingsFactory] Default entity-mode: pojo
19:23:28,264 INFO [SessionFactoryImpl] building session factory
19:23:28,342 WARN [Configurator] No configuration found. Configuring ehcache from
ehcache-failsafe.xml found in the classpath:
jar:file:/C:/jboss-4.0.2/server/default/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
19:23:29,420 INFO [SessionFactoryObjectFactory] Factory name: java:/HibernateFactory
19:23:29,420 INFO [NamingHelper] JNDI InitialContext properties:{}
19:23:29,420 INFO [SessionFactoryObjectFactory] Bound factory to JNDI name: java:/HibernateFactory
19:23:29,420 WARN [SessionFactoryObjectFactory] InitialContext did not implement EventContext
19:23:29,420 INFO [NamingHelper] JNDI InitialContext properties:{}
19:23:29,420 INFO [SessionFactoryImpl] Checking 0 named queries
May be some configuration file is missing, I post related xml files here.
Hope there are helpful to explain my mistake.
Code:
jboss-service.xml:--
<server>
<mbean code="org.hibernate.jmx.HibernateService" name="jboss.jca:service=Hibernate">
<depends>jboss.jca:service=RARDeployer</depends>
<attribute name="MapResources">com/pkg1/hibernate/Customer.hbm.xml</attribute>
<attribute name="JndiName">java:/HibernateFactory</attribute>
<attribute name="Datasource">java:/PostgresDS</attribute>
<attribute name="Dialect">org.hibernate.dialect.PostgreSQLDialect</attribute>
<attribute name="UserName">MC</attribute>
<attribute name="Password">12345</attribute>
<attribute name="UserTransactionName">UserTransaction</attribute>
<attribute name="TransactionStrategy">org.hibernate.transaction.JTATransactionFactory</attribute>
<attribute name="TransactionManagerLookupStrategy">org.hibernate.transaction.JBossTransactionManagerLookup</attribute>
</mbean>
</server>
Code:
hibernate.cfg.xml:--
<?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>
<property name="hibernate.connection.datasource">java:/PostgresDS</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<property name="hibernate.transaction.flush_before_completion">true</property>
<property name="hibernate.transaction.auto_close_session">true</property>
<property name="show_sql">true</property>
<mapping resource="com/pkg1/hibernate/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
Please help. Thank you very much.