13 Replies Latest reply on Jan 6, 2014 2:26 PM by Wolf-Dieter Fink

    how to lookup datasource within module

    Harald Hartmann Newbie

      Hello,

       

      how can i lookup datasource within a module?

       

      {code}

      String dsName = "java:/DB2";

      Context context = new InitialContext();

      DataSource dataSource = (DataSource) context.lookup(dsName);

      Connection connection = dataSource.getConnection();

      {code}

       

      works fine in a servlet deployed as war in standalone/deployments

       

      The same code fails with

      {noformat}Error while lookup: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial{noformat

      if i extract this snippet in a module and add this module as jboss-deployment-structure dependency in the war WEB-INF.

       

      Also unsuccessfully tried adding properties to InitialContext

      {code}

      Properties properties = new Properties();

      properties.put("java.naming.factory.initial", "org.jboss.as.naming.InitialContextFactory");

      {code}

       

      Some ideas?

       

      Regards

      Harald

       

      BTW: Anybody migrated from JBoss 4.2.3 to 7.1.1

        • 1. Re: how to lookup datasource within module
          Nicklas Karlsson Master

          I know I've done it with earlier AS7 versions (datasource-based custom log appender) but I was warned that there was no gurarantee it was future-proof. But I would expect the error to come later on, are you sure the hashmap was passed along correctly? Tried the system property alternative?

          1 of 1 people found this helpful
          • 2. Re: how to lookup datasource within module
            Harald Hartmann Newbie

            Nicklas Karlsson schrieb:

             

            ...are you sure the hashmap was passed along correctly? Tried the system property alternative?

             

            I did this here:

             

            {code}

            ...

            Properties properties = new Properties();

            properties.put("java.naming.factory.initial", "org.jboss.as.naming.InitialContextFactory");

            context = new InitialContext(properties);

            ...

            {code}

             

            surely adding org.jboss.as.naming to my module.xml

             

            {code:xml}

            <module xmlns="urn:jboss:module:1.1" name="de.haba.contextutil">

              <resources>

                <resource-root path="contextutil.jar"/>

              </resources>

              <dependencies>

                <module name="javax.naming" />

                <module name="org.jboss.as.naming" />

                <module name="org.apache.log4j" />

              </dependencies>

            </module>

            {code}

             

            results in

             

            {noformat}

            08:01:09,053 ERROR [de.haba.context.ContextUtil] (http--0.0.0.0-8080-2) javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.as.naming.InitialContextFactory [Root exception is java.lang.ClassCastException: org.jboss.as.naming.InitialContextFactory cannot be cast to javax.naming.spi.InitialContextFactory]: javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.as.naming.InitialContextFactory [Root exception is java.lang.ClassCastException: org.jboss.as.naming.InitialContextFactory cannot be cast to javax.naming.spi.InitialContextFactory]

                    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657) [rt.jar:1.6.0_26]

                    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) [rt.jar:1.6.0_26]

                    at javax.naming.InitialContext.init(InitialContext.java:223) [rt.jar:1.6.0_26]

                    at javax.naming.InitialContext.<init>(InitialContext.java:197) [rt.jar:1.6.0_26]

                    at de.haba.context.ContextUtil.getDataSource(ContextUtil.java:30) [contextutil.jar:]

            {noformat}

             

             

            No, the system property alternative i haven't tried yet.

            • 3. Re: how to lookup datasource within module
              Stephen Coy Master

              Chances are you need more than javax.naming as a dependency. Try javax.api instead.

              1 of 1 people found this helpful
              • 4. Re: how to lookup datasource within module
                Harald Hartmann Newbie

                Thanks a lot, it works now.

                 

                But this make me something like this:

                I thought to depend only on what you obviously need, but now i can do it like in the past (4.2.3): depend on everything to have no problems at all.

                Those are the days i'am asking myself giving up.

                • 5. Re: how to lookup datasource within module
                  Stephen Coy Master

                  I know what you mean, but sometimes you need to be pragmatic.

                   

                  You could narrow it down with trial and error, but javax.naming is provided by the JDK and it's internal workings may change resulting in other deps that happen to be in the same JDK.

                  • 6. Re: how to lookup datasource within module
                    Nicklas Karlsson Master

                    But this make me something like this:

                    I thought to depend only on what you obviously need, but now i can do it like in the past (4.2.3): depend on everything to have no problems at all.

                    Those are the days i'am asking myself giving up.

                     

                    You must be new in the field if that makes you want to give up

                    If you really want to be depressed, I recommend you start working with JSF and any of the major *Faces component libraries with client browser requirements of IE6.

                    • 7. Re: how to lookup datasource within module
                      Harald Hartmann Newbie

                      Yeah, i'm new in the field of 7.x, giving up in the manner of migrating 4.2.3 to 7.1.1. I think i should do everything new instead of migrating

                       

                      IE6 is definitely a challenge (and this also without JSF), you have my deepest sympathy

                      • 8. Re: how to lookup datasource within module
                        Gulam Samdani Expert

                        Remote Datasource connection with Jboss 7

                         

                        =============================================

                        // <datasource jndi-name="java:jboss/datasources/SampleDS" pool-name="SampleDS" enabled="true" use-java-context="true">

                          

                            public static void main(String[] args) throws SQLException {

                        DataSource cname = null;
                        Context context = null;

                          

                        try {
                        Properties properties = new Properties();
                        //    properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.as.naming.InitialContextFactory");
                        properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
                        context = new InitialContext(properties);
                        cname = (DataSource) context.lookup("java:jboss/datasources/SampleDS");
                        Connection connection = cname.getConnection();
                        System.out.println("connection-----M : "+ connection.isClosed());
                        } catch (NamingException e) {
                        e.printStackTrace();
                        }

                          

                            }

                         

                        ============================================

                        Error :

                         

                        javax.naming.NameNotFoundException: jboss/datasources/SampleDS --

                        service jboss.naming.context.java.jboss.exported.jboss.datasources.SampleDS

                        • 9. Re: how to lookup datasource within module
                          Wolf-Dieter Fink Master

                          Remote access to datasources are not supported in AS7 and further, as it is a bad idea.

                          • 10. Re: how to lookup datasource within module
                            Gulam Samdani Expert

                            can you provide alternative solution for this purpose ?

                             

                            is EJB as an intermediary in accessing the data source ??

                             

                            https://access.redhat.com/site/solutions/231013

                            • 11. Re: how to lookup datasource within module
                              Wolf-Dieter Fink Master

                              As you read this in the Red Hat knowledgebase you can be sure that this will be the recommended solution as it will be continuous reviewd by supporter an engineers

                              So yes use EJB's to wrap the database access.

                              • 12. Re: how to lookup datasource within module
                                Gulam Samdani Expert

                                Hi, i am trying to solve this problem using ejb's wrap to db connection  but I have got this problem ..

                                 

                                ------------------------------------------------------------------------------------

                                @Remote

                                public interface ProductServiceImplRemote {

                                 

                                    Connection getConnection();

                                    .......

                                   

                                   

                                    public static void main(String[] args) throws SQLException {

                                            DataSource cname = null;

                                            Context context = null;

                                            ProductServiceImplRemote bean = null;

                                            try {

                                                Properties properties = new Properties();

                                                properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

                                                context = new InitialContext(properties);

                                                bean = (ProductServiceImplRemote) context.lookup("ejb:/ejbdemo1//ProductServiceImpl!com.bus.ProductServiceImplRemote");

                                                 System.out.println(bean.getConnection());

                                                

                                                

                                                

                                                

                                                 Error :

                                                

                                                bean.getConnection   ..............Null

                                               

                                               

                                                jboss ejb side error is :

                                               

                                               

                                               

                                                org.jboss.marshalling.river.RiverMarshaller.doWriteObject(R

                                               

                                                 (EJB default - 2) Could not write method invocation result for method

                                                 public abstract java.sql.Connection com.bus.ProductServiceImplRemote.getConnection() on bean named

                                                

                                                 in object org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6@174632b

                                • 13. Re: how to lookup datasource within module
                                  Wolf-Dieter Fink Master

                                  Not that way.

                                  You can not pass the connection to a remote client neither by JNDI lookup nor by returning the connection.

                                   

                                  What is meant by wrapping the connection is to have all the code which need the connection reside inside the EJB.

                                  pass the parameter to the EJB, inside the EJB open the connection, manipulate the data and close the connection.

                                  You may return data or not.