Hello.
I made everything needed that is described in this article: Adobe Experience Manager Help | Querying MySQL data using an Adobe Experience Manager 6.4 DataSource...
I can successfuly get data source instance only after component activation. If I do it during the activation I get this error message: "No data source found with name 'MyDatabase' (after asking 0 providers)".
Here below is the source code. I have no errors getting dataSource if I only call getConnection() method. Activation method is problematic.
I use Adobe Experience Manager (6.2.0.SP1-CFP16) version.
Could I get some assistance? Thanks in advance.
================== THE SOURCE CODE ==================
package test.components;
import java.sql.Connection;
public interface DataBaseComponent {
Connection getConnection();
}
package test.components.impl;
import org.osgi.service.component.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import com.day.commons.datasource.poolservice.DataSourcePool;
import com.day.commons.datasource.poolservice.DataSourceNotFoundException;
import test.components.DataBaseComponent;
@Component (
immediate = true,
scope = ServiceScope.SINGLETON
)
public class DataBaseComponentImpl implements DataBaseComponent {
private static final String DATA_SOURCE_NAME = "MyDatabase";
@Reference
private DataSourcePool dataSourcePool;
private DataSource dataSource;
private Logger logger = LoggerFactory.getLogger(getClass());
public Connection getConnection() {
Connection con = null;
if (dataSource != null) {
try {
con = dataSource.getConnection();
return con;
} catch (Exception e) {
logger.error(e.getMessage());
}
} else {
try {
dataSource = (DataSource) dataSourcePool.getDataSource(DATA_SOURCE_NAME);
con = dataSource.getConnection();
logger.info("dataSource in instantiated ...");
} catch (Exception e) {
logger.error(e.getMessage());
}
return con;
}
return null;
}
@Activate
public void activate() {
try {
dataSource = (DataSource) dataSourcePool.getDataSource(DATA_SOURCE_NAME);
logger.info("activation step: dataSource in instantiated ...");
} catch(DataSourceNotFoundException e) {
logger.error(e.getMessage());
}
}
}
Solved! Go to Solution.
Views
Replies
Total Likes
As shown in the vidoe - you need the driver file bundle and OSGi bundle - both in Active state.
Views
Replies
Total Likes
In the article - we have this that works --
@Component
public
class
EmployeeImpl
implements
EmployeeInter {
I will show all working in a video
Just noticed that you said you have 6.2. This article is for 6.4.
Views
Replies
Total Likes
Hi,
I will test that article and post back my findings.
But all HELPX articles are tested by many community people and it always works!!
Thanks,
Ratna Kumar.
Views
Replies
Total Likes
I tested your code in 6.3, working fine for me. I am able to instantiated dataSource inside activate method.
you can find my code and invoking servlet at
TestDataBaseConnectionSevlet.java
Servlet output
Logs
Views
Replies
Total Likes
Hi Ratna,
That article should be fine because there is no Activate method to instantiated dataSource
Views
Replies
Total Likes
I just tested and it works as is (a big part of AEM HELPX community articles is we test them regularly to ensure they work).
A video will be posted here and in Article too.
Views
Replies
Total Likes
Thank you all for your answers. I supply PostreSQL driver by the same OSGi bundle. And at the time when my bundle gets reinstalled the JDBC driver does so too. Can it be the case?
Views
Replies
Total Likes
Here is the video that shows it all working....
We will also put this into the article too to show it all working...
Views
Replies
Total Likes
As shown in the vidoe - you need the driver file bundle and OSGi bundle - both in Active state.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies