Hi,

if you don't want spring to start the application context, move your spring files to another directory. You might also take a look at the spring-dm documentation about what you can do to influence the starting behavior.

BTW. do you know that you can let do spring take care of DataSources? You can even configure a datasource as a osgi-service and spring-dm or blueprint can take care of injecting this datasource service.

greetings, achim

https://github.com/ANierbeck/jmx-webconsole
Ok,  This is a doozy.

I'm using Karaf 2.0.0,  with SpringDM.

My OSGi application has a database-access bundle which contains two
applicationContext.xml files in the META-INF/spring directory.  One of these
xml files creates a bean called myAppDataSourceController, which provides
access to our database via the associated pojo.  All this deploys properly
and I'm able to verify the database connection.

To test whether it works, I've created a simple Spring testHarness
consisting of one class (DAOTest.java) which attempts to use my
database-access bundle to connect to the database and write a column in a
table. It als has a testApplicationContext.xml file which creates a bean,
and references the method that begins the test in DAOTest.java.  This is
failing.

In my old non-osgi implementation, I had a DataSourceControllerFactory class
whose static final inner-class (named Holder) looks as such:

// non-important bits removed
private static final class Holder {

   private static final DataSourceController INSTANCE;

   static {
    final String[] contexts = {
     "META-INF/spring/ControllerAppContext.xml",
     "META-INF/spring/DataSourceAppContext.xml" };

    final ApplicationContext ctx = new
ClassPathXmlApplicationContext(contexts);
    INSTANCE = {DataSourceController}
ctx.getBean("myAppDataSourceController");

   }

}

I use this class to enforce the singleton pattern we are using.  The
enclosing class has one major method:
public static final DataSourceController getInstance() {
   try {
    return Holder.INSTANCE;
   } catch statement stuff...

}

When another part of the application tried to access the database, it would
call DataSourceControllerFactory.getInstance(), to get a copy of the
DataSourceController which would provide database access.

Is there a similar way to access a database from within Karaf?  Obviously,
this way isn't going to work as SpringDM evaluates the context.xml files,
and creates a connection before the above methods are called. In fact,
calling the above methods results in an attempt to read the context files
again, but from the module attempting to get a handle to the database
connection.

Anyhow, before this gets too long, I'm just looking for OSGi friendly
suggestions to access our database.  Can anyone help me out?

Mike Van

Reply via email to