Well with the karaf server and spring it's quite easy.
just make a single spring xml config file and deploy this.
The needed configuration could looks like this:
taken from the springsource documentation for a datasource
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
now you need also the commons dbcp and commons pool in your deploy
directory.
for configuration you could go and use this
<context:property-placeholder location="jdbc.properties"/>
or better use the configuration admin service
more details on how to use this can be found in the very good
documentation of spring-dm
now how to publish the bean as a service,
you just need to make sure you also have the spring-dm configure (but
make sure it is 1.2 because this is the one supported by the karaf)
<service ref="dataSource" interface="java.sql.DataSource"/>
Now you have a service for your datasource.
Now with your other code just let the datasource be injected
<reference id="myDataSourceReference" interface="java.sql.DataSource"
filter="(here might be a filter for example to select a certain DataSource
Pool)"/>
Hope that helps you around, again take a good look at the documentation
for spring-dm.
Blueprint is the same concerning the configuration above.
Cheers, Achim
Am 19.11.2010 22:45, schrieb Mike Van:
Achim,
That's very interesting. I'd be interested in seeing examples of what your'e
talking about, if you can point me in the right direction.
v/r,
Mike Van
----- Original Message -----
From: "Achim Nierbeck [via
Karaf]"<[email protected]>
To: "Mike Van"<[email protected]>
Sent: Friday, November 19, 2010 4:15:59 PM
Subject: Re: Accessing database connections using SpringDM in Karaf
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
View message @
http://karaf.922171.n3.nabble.com/Accessing-database-connections-using-SpringDM-in-Karaf-tp1932916p1932992.html
To start a new topic under Karaf - User, email
[email protected]
To unsubscribe from Karaf - User, click here .