Connecting MS SQL Database from 5.6 using OSGi Service
Hi Everyone,
I am trying to connect to MS SQL database in AEM 5.6 . Below is my service class which will provide the connection.
@Component
@Service
public class JDBCConnectionServiceImpl implements JDBCConnectionService{
private static final Logger LOGGER =
LoggerFactory.getLogger(JDBCConnectionServiceImpl.class);
private static String DATA_SOURCE_NAME="sqljdbc";
@Reference
private DataSourcePool source;
public Connection getConnection() {
Connection con=null;
try {
DataSource ds = (DataSource) this.source.getDataSource(DATA_SOURCE_NAME);
con= ds.getConnection();
}catch(Exception e){
LOGGER.error("Exception when getting the setting data source ",e);
}
return con;
}
public void closeConnection(Connection connection) {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
LOGGER.error("Exception when trying to close the connection {} ",e);
}
}}
When I try to access this service it throws a class not found exception as shown
java.lang.ClassNotFoundException: mypackage.JDBCConnectionServiceImpl not found by mypackage [377]
at java.lang.Throwable.<init>(Throwable.java:80)
at java.lang.ClassNotFoundException.<init>(ClassNotFoundException.java:76)
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1420)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1923)
But when I access the same data source in component JSP as shown below it is working fine
DataSourcePool dspService = sling.getService(DataSourcePool.class);
DataSource ds = (DataSource) dspService.getDataSource("sqljdbc");
Not sure what is the problem with OSGi Service.
Help me on this please.
Regards,
Krishna