Hi All. 
I am stuck in a peculiar problem and struggling with it. I have a created a 
standalone program in Java that accesses a database through a DataSource which 
I have configured in JBoss AS. I am trying to access the database using JNDI. 
I have a program by the name of SaveExample 


  | private static Context jndiContext;
  |     
  |     public static void main(String[] args) {
  |             // TODO Auto-generated method stub
  |             
  |             
  |             Session session = null;
  |             
  |             try{
  |                     System.out.println("Before Session Factory 
Configuration. !!!");
  |                     
  |                     SessionFactory sf = new 
Configuration().configure().buildSessionFactory();
  |                     
  |                     
  |                     jndiContext = new InitialContext();
  |                     
  |                     sf = (SessionFactory)jndiContext.lookup("java:MySqlDS");
  |                     
  |                     
  |                     session = sf.openSession();
  |                     
  |                     System.out.println("After Session Factory 
Configuration. !!!");
  |                     
  |                     
  |                     
  |             }catch(Exception e){
  |                     e.printStackTrace();
  |             }finally{
  |                     session.flush();
  |                     session.close();
  |             }
  | 
  |     }
  | 
  | 

hibernate.cfg.xml
--------------------

  | <hibernate-configuration>
  | 
  |     <session-factory>
  |             
  |             <property 
name="hibernate.connection.datasource">MySqlDS</property> 
  |             <property 
name="hibernate.session_factory_name">java:Mydatabase</property>
  |         <property name="hibernate.jndi.url"></property>
  |             <property name="hibernate.jndi.class"></property>
  |             <property name="hibernate.connection.username"></property>
  |             <property name="hibernate.connection.password"></property>
  |             
  |             <property name="show_sql">true</property>
  |             <property name="format_sql">true</property>
  |             
  |             <!-- Show and print nice SQL on stdout. -->
  |             <property 
name="dialect">org.hibernate.dialect.MySQLDialect</property>
  |             <property name="hibernate.hbm2ddl.auto">update</property>
  |             
  |             <!-- List of XML Mapping files -->
  |             <mapping resource="contact.hbm.xml"/>
  | <!--        <mapping resource="address.hbm.xml"/> -->
  |     
  |     </session-factory>
  |     
  | </hibernate-configuration>
  | 
  | 

I have put the lib file mysql-connector-java-5.1.5-bin.jar in the 
server\default\lib folder of JBoss and I have put the mysql-ds.xml file in 
server\default\deploy directory of JBoss. 

mysql-ds.xml 
---------------

  | 
  | <datasources>
  |   <local-tx-datasource>
  |     <jndi-name>MySqlDS</jndi-name>
  |     <connection-url>jdbc:mysql://localhost:3306/jbossdb</connection-url>
  |     <!-- Added by me for configuting a data source for remote usage -->
  |     <use-java-context>false</use-java-context>
  |     <driver-class>com.mysql.jdbc.Driver</driver-class>
  |     <user-name>root</user-name>
  |     <password>root</password>
  |     
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
  |     <!-- should only be used on drivers after 3.22.1 with "ping" support
  |     
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
  |     -->
  |     <!-- sql to call when connection is created
  |     <new-connection-sql>some arbitrary sql</new-connection-sql>
  |       -->
  |     <!-- sql to call on an existing pooled connection when it is obtained 
from pool - MySQLValidConnectionChecker is preferred for newer drivers
  |     <check-valid-connection-sql>some arbitrary 
sql</check-valid-connection-sql>
  |       -->
  | 
  |     <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
  |     <metadata>
  |        <type-mapping>mySQL</type-mapping>
  |     </metadata>
  |   </local-tx-datasource>
  | </datasources>
  | 
  | 
  | 

Exception 
-----------

  | 
  | Before Session Factory Configuration. !!!
  | log4j:WARN No appenders could be found for logger 
(org.hibernate.cfg.Environment).
  | log4j:WARN Please initialize the log4j system properly.
  | org.hibernate.HibernateException: Could not find datasource
  |     at 
org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:56)
  |     at 
org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
  |     at 
org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:56)
  |     at 
org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:414)
  |     at 
org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
  |     at 
org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
  |     at 
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
  |     at com.example.jndi.JNDIExample.main(JNDIExample.java:27)
  | Caused by: javax.naming.NoInitialContextException: Cannot instantiate 
class:  [Root exception is java.lang.ClassNotFoundException: ]
  |     at 
javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)
  |     at 
javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
  |     at javax.naming.InitialContext.init(InitialContext.java:223)
  |     at javax.naming.InitialContext.<init>(InitialContext.java:197)
  |     at 
org.hibernate.util.NamingHelper.getInitialContext(NamingHelper.java:28)
  |     at 
org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
  |     ... 7 more
  | Caused by: java.lang.ClassNotFoundException: 
  |     at java.lang.Class.forName0(Native Method)
  |     at java.lang.Class.forName(Class.java:242)
  |     at 
com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:42)
  |     at 
javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)
  |     ... 12 more
  | Exception in thread "main" java.lang.NullPointerException
  |     at com.example.jndi.JNDIExample.main(JNDIExample.java:44)
  | 
  | 


My problem is that the program (SaveExample.java) in Eclipse is running as a 
Java Application and Jboss server is running in another console window. So how 
could I make them connect to each other so that they the application program 
can recognize the datasource and not given Could not find datasource exception. 

View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4165498#4165498

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4165498
_______________________________________________
jboss-user mailing list
jboss-user@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/jboss-user

Reply via email to