0

When my application tried to connect to database via datasource using jndi,it shows that my datasource is null. below is all the relevant code.

XML file

 <?xml version="1.0" encoding="UTF-8"?>
    <server description="data source configuration">
        <dataSource id="sybase"
                    jndiName="jdbc/sybase"
                    type="javax.sql.DataSource"
        >
            <jdbcDriver>
                <library>
                    <fileset dir="${ibs.dbdrivers.dir}" includes="jconn3.jar"/>
                </library>
            </jdbcDriver>

            <properties.sybase databaseName="XXX" URL="jdbc:sybase:Tds:XXX:5000"/>
            <recoveryAuthData user="XX" password="XX"/>
        </dataSource>
    </server>

Configration class

@Configuration
public class SybaseDataConfig {

  @Bean
  public DataSource localDataSource() {
    JndiTemplate jndi = new JndiTemplate();
    try {
      return (DataSource) jndi.lookup("java:comp/env/jdbc/sybase");
    } catch (NamingException e) {
      throw new BeanCreationException("dataSource", "Error looking up data source", e);
    }
  } 
}

DAOimp class

public class DAOimp implements IDAOimp{

  private JdbcTemplate jdbcTemplate;

  private SybaseDataConfig sybase;
  @Autowired
  public void setDataSource(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(sybase.localDataSource());
  }
  //some code  

}

ibm-web-bnd.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-bnd xmlns="http://websphere.ibm.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd"
         version="1.0">
    <virtual-host name="default_host"/>
    <resource-ref name="jdbc/sybase" binding-name="jdbc/sybase"/>
</web-bnd>

web.xml

<resource-ref>
        <res-ref-name>jdbc/sybase</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

when my controller call the method in the daoimpl class, it says that the datasource is null. may i know what is wrong and what is the solution. thanks in advance!

UPDATE:

   <featureManager>
        <feature>webProfile-6.0</feature>
        <feature>jpa-2.0</feature>
        <feature>ssl-1.0</feature>
        <feature>localConnector-1.0</feature>
        <feature>jdbc-4.0</feature> 
    </featureManager>
  • 2
    Cant you just use @Resource and inject DataSource where you need it instead all this spring stuff?? – Gas Feb 03 '17 at 11:30
  • @Gas same..i use resource but it also couldnt find the jndiname – fishermanfriends Feb 08 '17 at 10:14
  • Add full`server.xml` to the question or at least the features part. Do you have jdbc and jndi features in it? – Gas Feb 08 '17 at 11:36
  • Added the features part. did i miss something there – fishermanfriends Feb 08 '17 at 11:54
  • Looks ok. I'd check 2 things: look in the messages.log file during server startup, and application startup if there are any exceptions; try to update Sybase driver to jconn4 and see if it makes any difference. – Gas Feb 08 '17 at 13:55
  • no luck using jconn4 either. in the message log there is nothing there is unusual. – fishermanfriends Feb 08 '17 at 15:12
  • one thing i do notice however is that my configurations class is not getting picked up when starting the server as i check in my message.log there is no "SybaseDataConfig" there – fishermanfriends Feb 08 '17 at 15:19
  • I configured various datasources for Liberty and didnt have such issues. So really dont have any hits for you, rather than trying to create plain servlet to verify that datasource config is correct. Then you can try to solve your spring issues. – Gas Feb 10 '17 at 09:32

1 Answers1

2

To narrow down the cause of the problem, first confirm that your server XML configuration is valid (it looks correct) by temporarily switching to a direct lookup:

return (DataSource) javax.naming.InitialContext.doLookup("jdbc/sybase");

If unsuccessful, you should see an exception that helps identify the cause, or a general naming exception, in which case check the server logs for any warnings or errors.

If successful, then confirm that your deployment descriptor and bindings are correct by temporarily switching to an indirect lookup,

return (DataSource) javax.naming.InitialContext.doLookup("java:comp/env/jdbc/sybase");

If successful, you'll need someone with Spring expertise to answer the question.

If unsuccessful, the deployment descriptor or bindings may be incorrect (they look correct) or placed in a wrong location (they belong under WEB-INF), or possibly the localDataSource method might not be running on a thread that is associated with the web module to which the bindings pertain.

njr
  • 3,399
  • 9
  • 7
  • when i move the bean part of my configuration to the service class, there is an error which says "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". i have decared the jndiName in the web.xml and ibm-web-bnd.xml. i do not know what is wrong here.. – fishermanfriends Feb 08 '17 at 10:13
  • Just write a simple servlet with these lookups for testing. Or even better create separate project/application, without any spring related libraries. – Gas Feb 08 '17 at 11:38
  • If you are getting NoInitialContextException, then you are almost certainly not running on a web container thread. Running with the context of a web container thread is a requirement of being able to access the resource reference that you have defined in web.xml. It looks like we will need someone with knowledge about Spring to answer how to connect @Configuration/@Bean up to a web container request which will make it possible for the resource references you defined to be accessible. – njr Feb 09 '17 at 14:50