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>