1 Reply Latest reply on Jul 19, 2013 8:10 AM by wdfink

    Connect to MySQL without Datasource in JBoss AS 7.1

    pradeepsixer

      Hi,

       

      I am using JBoss AS 7.1 and am trying to connect to MySQL database without creating a datasource name. Unfortunately, while running the client program,  the Bean throws ClassNotFoundException

       

      Here is what I did :

      I have put the mysql-connector-java-5.1.18-bin.jar in JBOSS_HOME/modules/com/mysql/jdbc/main . And created a module.xml file with the following contents :

       

      <?xml version="1.0" encoding="UTF-8"?>
      <module xmlns="urn:jboss:module:1.0" name="com.mysql.jdbc">
        <resources>
          <resource-root path="mysql-connector-java-5.1.18-bin.jar"/>
        </resources>
        <dependencies>
          <module name="javax.api"/>
        </dependencies>
      </module>
      

       

      Then updated the standalone.xml file with the following line under the drivers node

       

      <driver name="mysql" module="com.mysql.jdbc"/>
      

       

      Here is my Bean Code :

       

      package com.testing.dbaccess;
      
      
      import javax.ejb.Stateless;
      
      
      /**
       * Session Bean implementation class DatabaseAccess
       */
      @Stateless
      public class DatabaseAccess implements DatabaseAccessRemote
      {
      
      
                /**
                 * Default constructor.
                 */
                public DatabaseAccess()
                {
                          // TODO Auto-generated constructor stub
                }
      
      
                public void testConnection()
                {
                          try
                          {
                                    Class.forName("com.mysql.jdbc.Driver");
                                    System.out.println("Success");
                          }
                          catch (ClassNotFoundException e)
                          {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                          }
                }
      
      
      }
      

       

       

      And my interface for the bean would be :

       

      package com.testing.dbaccess;
      
      
      import javax.ejb.Remote;
      
      
      @Remote
      public interface DatabaseAccessRemote
      {
                public void testConnection();
      }
      

       

      My Client program is :

       

      package com.testing.client;
      
      
      import com.testing.dbaccess.*;
      
      
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      
      
      import java.util.Properties;
      
      
      public class DBClient
      {
                public static void main(String a[])
                {
                          DatabaseAccessRemote dbaccess = searchBean();
                          dbaccess.testConnection();
                }
        
                private static DatabaseAccessRemote searchBean()
                {
                          Context initialContext;
                          Properties properties = new Properties();
                          DatabaseAccessRemote bean = null;
                          properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
                          try
                          {
                                    initialContext = new InitialContext(properties);
                                    bean = (DatabaseAccessRemote) initialContext
                                                        .lookup("ejb:/testingEjb//DatabaseAccess!com.testing.dbaccess.DatabaseAccessRemote");
                          }
                          catch (NamingException e)
                          {
                                    e.printStackTrace();
                          }
                          return bean;
        
                }
      }
      
      

       

       

      And i get the following error when i run the client :

       

      17:30:17,864 ERROR [stderr] (EJB default - 1) java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from [Module "deployment.testingEjb.jar:main" from Service Module Loader]
      17:30:17,867 ERROR [stderr] (EJB default - 1)           at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)
      17:30:17,868 ERROR [stderr] (EJB default - 1)           at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:468)
      17:30:17,869 ERROR [stderr] (EJB default - 1)           at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:456)
      17:30:17,871 ERROR [stderr] (EJB default - 1)           at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
      17:30:17,872 ERROR [stderr] (EJB default - 1)           at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:120)
      17:30:17,874 ERROR [stderr] (EJB default - 1)           at java.lang.Class.forName0(Native Method)
      17:30:17,875 ERROR [stderr] (EJB default - 1)           at java.lang.Class.forName(Class.java:186)
      17:30:17,876 ERROR [stderr] (EJB default - 1)           at com.testing.dbaccess.DatabaseAccess.testConnection(DatabaseAccess.java:24)
      17:30:17,877 ERROR [stderr] (EJB default - 1)           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      17:30:17,878 ERROR [stderr] (EJB default - 1)           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      17:30:17,879 ERROR [stderr] (EJB default - 1)           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      17:30:17,881 ERROR [stderr] (EJB default - 1)           at java.lang.reflect.Method.invoke(Method.java:601)
      17:30:17,882 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ee.component.ManagedReferenceMethodInterceptorFactory$ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptorFactory.java:72)
      17:30:17,884 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:17,885 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
      17:30:17,887 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:36)
      17:30:17,888 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:17,889 ERROR [stderr] (EJB default - 1)           at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:47)
      17:30:17,896 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:17,897 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)
      17:30:17,900 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:17,901 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
      17:30:17,902 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:53)
      17:30:17,904 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:17,906 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51)
      17:30:17,915 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:17,916 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:228)
      17:30:17,918 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:304)
      17:30:17,919 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:190)
      17:30:17,920 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:17,921 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ejb3.remote.EJBRemoteTransactionPropagatingInterceptor.processInvocation(EJBRemoteTransactionPropagatingInterceptor.java:80)
      17:30:17,922 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:17,923 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)
      17:30:17,925 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:17,926 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:59)
      17:30:17,927 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:17,928 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)
      17:30:17,929 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:17,930 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:43)
      17:30:17,932 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:17,933 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)
      17:30:17,934 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:17,935 ERROR [stderr] (EJB default - 1)           at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
      17:30:17,936 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:165)
      17:30:17,937 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ejb3.remote.protocol.versionone.MethodInvocationMessageHandler.invokeMethod(MethodInvocationMessageHandler.java:302)
      17:30:17,939 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ejb3.remote.protocol.versionone.MethodInvocationMessageHandler.access$200(MethodInvocationMessageHandler.java:64)
      17:30:17,940 ERROR [stderr] (EJB default - 1)           at org.jboss.as.ejb3.remote.protocol.versionone.MethodInvocationMessageHandler$1.run(MethodInvocationMessageHandler.java:196)
      17:30:17,942 ERROR [stderr] (EJB default - 1)           at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
      17:30:17,943 ERROR [stderr] (EJB default - 1)           at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
      17:30:17,944 ERROR [stderr] (EJB default - 1)           at java.util.concurrent.FutureTask.run(FutureTask.java:166)
      17:30:17,945 ERROR [stderr] (EJB default - 1)           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
      17:30:17,946 ERROR [stderr] (EJB default - 1)           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
      17:30:17,947 ERROR [stderr] (EJB default - 1)           at java.lang.Thread.run(Thread.java:722)
      17:30:17,948 ERROR [stderr] (EJB default - 1)           at org.jboss.threads.JBossThread.run(JBossThread.java:122)
      17:30:31,262 ERROR [stderr] (EJB default - 2) java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from [Module "deployment.testingEjb.jar:main" from Service Module Loader]
      17:30:31,264 ERROR [stderr] (EJB default - 2)           at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)
      17:30:31,265 ERROR [stderr] (EJB default - 2)           at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:468)
      17:30:31,266 ERROR [stderr] (EJB default - 2)           at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:456)
      17:30:31,268 ERROR [stderr] (EJB default - 2)           at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
      17:30:31,269 ERROR [stderr] (EJB default - 2)           at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:120)
      17:30:31,270 ERROR [stderr] (EJB default - 2)           at java.lang.Class.forName0(Native Method)
      17:30:31,271 ERROR [stderr] (EJB default - 2)           at java.lang.Class.forName(Class.java:186)
      17:30:31,272 ERROR [stderr] (EJB default - 2)           at com.testing.dbaccess.DatabaseAccess.testConnection(DatabaseAccess.java:24)
      17:30:31,273 ERROR [stderr] (EJB default - 2)           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      17:30:31,274 ERROR [stderr] (EJB default - 2)           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      17:30:31,275 ERROR [stderr] (EJB default - 2)           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      17:30:31,276 ERROR [stderr] (EJB default - 2)           at java.lang.reflect.Method.invoke(Method.java:601)
      17:30:31,277 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ee.component.ManagedReferenceMethodInterceptorFactory$ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptorFactory.java:72)
      17:30:31,279 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:31,280 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
      17:30:31,281 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:36)
      17:30:31,283 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:31,284 ERROR [stderr] (EJB default - 2)           at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:47)
      17:30:31,285 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:31,286 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)
      17:30:31,287 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:31,289 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
      17:30:31,294 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:53)
      17:30:31,295 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:31,296 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51)
      17:30:31,297 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:31,298 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:228)
      17:30:31,299 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:304)
      17:30:31,300 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:190)
      17:30:31,300 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:31,301 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ejb3.remote.EJBRemoteTransactionPropagatingInterceptor.processInvocation(EJBRemoteTransactionPropagatingInterceptor.java:80)
      17:30:31,302 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:31,303 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)
      17:30:31,304 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:31,305 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:59)
      17:30:31,306 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:31,307 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)
      17:30:31,308 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:31,308 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:43)
      17:30:31,309 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:31,310 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)
      17:30:31,311 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
      17:30:31,312 ERROR [stderr] (EJB default - 2)           at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
      17:30:31,313 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:165)
      17:30:31,314 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ejb3.remote.protocol.versionone.MethodInvocationMessageHandler.invokeMethod(MethodInvocationMessageHandler.java:302)
      17:30:31,315 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ejb3.remote.protocol.versionone.MethodInvocationMessageHandler.access$200(MethodInvocationMessageHandler.java:64)
      17:30:31,316 ERROR [stderr] (EJB default - 2)           at org.jboss.as.ejb3.remote.protocol.versionone.MethodInvocationMessageHandler$1.run(MethodInvocationMessageHandler.java:196)
      17:30:31,317 ERROR [stderr] (EJB default - 2)           at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
      17:30:31,318 ERROR [stderr] (EJB default - 2)           at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
      17:30:31,318 ERROR [stderr] (EJB default - 2)           at java.util.concurrent.FutureTask.run(FutureTask.java:166)
      17:30:31,319 ERROR [stderr] (EJB default - 2)           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
      17:30:31,320 ERROR [stderr] (EJB default - 2)           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
      17:30:31,321 ERROR [stderr] (EJB default - 2)           at java.lang.Thread.run(Thread.java:722)
      17:30:31,321 ERROR [stderr] (EJB default - 2)           at org.jboss.threads.JBossThread.run(JBossThread.java:122)
      

       

      Please help me out with this.

       

      Thanks in advance