DataSource in an OSGI container - java

I have a simple Spring App that connects to a DB via an EntityManager
So I have to following configuration:
<bean id="domainEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="TheManager" />
<property name="dataSource" ref="domainDataSource" />
<property name="packagesToScan" value="com.conztanz.persistence.stock.model" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>
<bean id="domainDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5433/dbName" />
<property name="username" value="xxxx" />
<property name="password" value="xxxx" />
</bean>
This works fine when lunched via a main class (loading the AppContext manually)
But, once deployed into ServiceMix I get the following error :
Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [org.postgresql.Driver]
I've read somewhere that OSGI and DriverManager don't mix well but I fail to understand why.
The solution that seems to be a good practice is to expose the dataSource as an OSGI bundle, do you agree ? and in that case how would you have access to it from a spring context to be able to have an EntityManager for example ?

DriverManager does not work well in OSGi. The easiest way is to use a DataSource directly. Most DB drivers have such a class. If you instantiate it in your app context then it will work. The downside is though that it binds your application to the DB driver as it then will import the packages for the DataSource impl.
A more loosely coupled way is to use ops4j pax jdbc. It allows to create a DataSource as an OSGi service from a config in config admin. So in your app context you just have to add a dependency to a DataSource service. So your application is not bound to the specific DB driver. One typical use case is to use H2 in tests and oracle in production.

Related

Spring 3: Postpone bean instanciation

I have a bean that uses a JNDI Connection Factory like this:
<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate"/>
<property name="jndiName" value="SOME_NAME"/>
</bean>
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">org.ow2.carol.jndi.spi.MultiOrbInitialContextFactory</prop>
<prop key="java.naming.provider.url">rmi://some_remote_host:1091</prop>
</props>
</property>
</bean>
The fact is that this connectionFactory tries to connect to the remote host as soon as the context is loaded. The problem is that the remote service is supposed to start after the context is loaded, the context fails to load, and the application fails to deploy. Would it be possible to postpone the connection of the factory to the moment when the connection is really needed?
In other words, the connection to the connection factory is eager, and I would like it to be lazy. Is there a way to achieve this appart from subclassing the Factory and hacking around?
A bean can be made lazy-initialized with the attribute lazy-init="true"
From the documentation
A lazy-initialized bean tells the IoC container to create a bean
instance when it is first requested, rather than at startup.
Also note that this behaviour changes when the lazy-initialized bean is a dependency of a singleton bean that is not lazy-initialized.
Example configuration
<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName" value="SOME_NAME"/>
</bean>

How access session factory (dataSource) in alfresco?

Working with common spring application I can define dataSource or Hibernate session factory as bean and then simply autowire them. like that:
#Autowired
private DataSource dataSource;
or
#Autowired
private SessionFactory sessionFactory;
For now I want to create some tables and store them in alfresco. How get dataSource or sessionFactory (if alfresco uses hibernate) which is pointed to alfresco database?
Inject the dataSource bean defined in core-services-context.xml:
<bean id="dataSource" class="org.alfresco.config.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/dataSource</value>
</property>
<property name="defaultObject">
<ref bean="defaultDataSource" />
</property>
</bean>
using your-config-context.xml, for example ibatis-context.xml:
<bean id="repoSqlSessionFactory" class="org.alfresco.ibatis.HierarchicalSqlSessionFactoryBean">
<property name="resourceLoader" ref="dialectResourceLoader"/>
<property name="dataSource" ref="dataSource"/>
<property name="configLocation">
<value>classpath:alfresco/ibatis/alfresco-SqlMapConfig.xml</value>
</property>
</bean>
Be aware that:
Alfresco incorporates Hibernate and iBATIS open source database
abstraction layers to allow it to be ported to a number of different
database management systems.
so, it is preferred to use MyBatis-Spring and org.mybatis.spring.SqlSessionTemplate, for example org.alfresco.repo.domain.audit.ibatis.AuditDAOImpl.java:
<bean id="auditSqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="repoSqlSessionFactory"/>
</bean>
<bean id="auditDAO" class="org.alfresco.repo.domain.audit.ibatis.AuditDAOImpl">
<property name="sqlSessionTemplate" ref="auditSqlSessionTemplate"/>
<property name="contentService" ref="contentService"/>
<property name="contentDataDAO" ref="contentDataDAO"/>
<property name="propertyValueDAO" ref="propertyValueDAO"/>
</bean>
This Alfresco 5 and Spring tutorial might be helpful for you.

Spring #Transactional on one service method spanning over two Hibernate transaction managers

I was wondering if it is possible to use two transaction manager in one service methods.
Because due to the limitation of client's mysql db configuration, we have got 2 different datasources within one database, i.e., one user/pwd/url per schema. Thats why i have to configured two transaction managers. Now I got problem when it comes to the service implementation. See the following code:
public class DemoService{
...
#Transactional(value = "t1")
public doOne(){
doTwo();
}
#Transactional(value = "t2")
public doTwo(){
}
...
}
if I using this code pattern, i always got the exception
org.hibernate.HibernateException: No Session found for current thread
If i run the two methods seperately, it workd fine.
Did i miss something? Or there is other work around here?
Any advice would be appreciated.
btw: some of my configuration
<bean id="sessionFactorySso" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="mappingLocations">
<list>
<value>classpath*:sso.vo/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="generateDdl">true</prop>
<prop key="hibernate.dialect">${dialect} </prop>
</props>
</property>
<property name="dataSource" ref="dataSourceSso"/>
</bean>
<bean id="dataSourceSso" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${driver}"/>
<property name="jdbcUrl" value="${sso.url}"/>
<property name="user" value="${sso.username}"/>
<property name="password" value="${sso.password}"/>
<!-- these are C3P0 properties -->
<property name="acquireIncrement" value="2" />
<property name="minPoolSize" value="1" />
<property name="maxPoolSize" value="2" />
<property name="automaticTestTable" value="test_c3p0" />
<property name="idleConnectionTestPeriod" value="300" />
<property name="testConnectionOnCheckin" value="true" />
<property name="testConnectionOnCheckout" value="true" />
<property name="autoCommitOnClose" value="true" />
<property name="checkoutTimeout" value="1000" />
<property name="breakAfterAcquireFailure" value="false" />
<property name="maxIdleTime" value="0" />
</bean>
<bean id="transactionManagerSso" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactorySso"/>
<qualifier value="sso" />
</bean>
<tx:annotation-driven transaction-manager="transactionManagerSso" />
Because you want to enlist two data sources in one transaction you need XA(Global) Transaction.
Therefore you need to:
Set the Spring JTA transaction manager
You Hibernate properties should use the JTA platform settings
Your data source connections should be XA complaint
You need an application server JTA transaction manager or a stand-alone tarnsaction manager (Bitronix, Atomikos, JOTM)
You will need two session factory configurations, one for each individual data source.
And you won't have two transaction managers: t1 and t2, but instead you will enlist two transactional XA data sources that will be automatically enlisted in the same global transaction, meaning you will have two XA connections being enlisted in the same global transaction. The XA transaction will use the 2PC protocol to commit both resources upon commit time.
Checkout this Bitronix Hibernate example.
You have a few options:
Inject the bean into itself and use the reference to call doTwo(). This really goes against the whole idea of IoC and AOP so I don't recommend it.
Switch to compile time weaving. Rather than using proxies, Spring (actually the AspectJ compiler) will add the bytecode to start/stop transactions to your class at compile time. There are pros and cons to this approach. See this page for more details.
Use load time weaving. Same as #2 except that your classes are modified as they are loaded rather than at compile time. IMO, Java classloading is complicated enough. I'm sure this works great for some folks but I personally would avoid this.
As Vlad pointed out, you can use JTA and XA.
Start a new transaction against transaction manager 2 within doOne() before calling doTwo(). RTFM on programmatic transaction management.
Check out ChainedTransactionManager. It essentially aggregates multiple transaction managers and does a "best effort" with commit/rollback. This is NOT a true two-phase commit like Vlad's solution.
All of these except for Vlad's solution (#4) have the potential to leave the databases in an inconsistent state. You need to use JTA/XA/two-phase commit to ensure consistency in the event that one of the TX managers throws an exception at commit time.

how to get a spring declared datasource in an xml config file

i have a spring datasource which looks like this:
<bean id="dataSource1" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database1.url}" />
<property name="username" value="${database1.username}" />
<property name="password" value="${database1.password}" />
</bean>
i need to make this available on jndi or jee. some related things:
<bean id="dataSourceJNDI1" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/dataSource1"/>
<property name="beanClassLoader" ref="dataSource1"></property>
</bean>
or maybe :
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/database1" />
not sure how to get the jndi or jee working with the dbcp. any help would be appreciated.
thanks in advance.
ps: guys this is a special needl. so i have to do it like this. please dont post unnecessary advices saying why i should use tomcat server as datasource. i am aware of setting datasource that way. i repeat again this is a special need. also please dont provide me java code solutions, not required.
ps: those who dont know how to do it, please do not occupy the space of this post saying its not possible. if you dont know the answer no need to post and junk the post.
You would need to bind the DataSource to the JNDI tree. You would need to supply the parameters for connecting to the local JNDI tree. JndiTemplate can do this. Some of the JNDI environment properties probably won't be necessary for a local InitialContext. I think "java.naming.factory.initial" is the only required. The other are for connecting to an out of process JNDI server:
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.provider.url">${jndi.provider.url}</prop>
<prop key="java.naming.factory.initial">${jndi.factory.initial}</prop>
<prop key="java.naming.security.principal">${jndi.security.principal}</prop>
<prop key="java.naming.security.credentials">${jndi.security.credentials}</prop>
</props>
</property>
</bean>
<bean factory-bean="jndiTemplate" factory-method="bind">
<constructor-arg type="java.lang.String" value="java:com/env/DataSoure"/>
<constructor-arg type="java.lang.Object" ref="dataSource"/>
</bean>
If you are performing a JNDI lookup in the same Spring context, you will either need to have the JNDI bean depends-on this lookup bean or make the JNDI lookup lazy so that it will perform the lookup on first use.
Since you are using jndi you have to declare the datasource as a jndi source.
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/database1" />
<bean id="dataSource1" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="dataSource" ref="dataSource" />
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database1.url}" />
<property name="username" value="${database1.username}" />
<property name="password" value="${database1.password}" />
</bean>
That should work assuming your bean definition for dataSource1 is correct.

How can you connect to a password protected MS Access Database from a Spring JdbcTemplate?

I need to connect to a password protected MS Access 2003 DB using the JDBC-ODBC bridge. I can't find out how to specify the password in the connect string, or even if that is the correct method of connecting.
It would probably be relevant to mention that this is a Spring App which is accessing the database through a JdbcTemplate configured as a datasource bean in our application context file.
Some relevant snippets:
from application-context.xml
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="legacyDataSource" />
</bean>
<bean id="jobsheetLocation" class="java.lang.String">
<constructor-arg value="${jobsheet.location}"/>
</bean>
<bean id="legacyDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.legacy.driverClassName}" />
<property name="url" value="${jdbc.legacy.url}"/>
<property name="password" value="-------------" />
</bean>
from our build properties
jdbc.legacy.driverClassName=sun.jdbc.odbc.JdbcOdbcDriver
jdbc.legacy.url=jdbc:odbc:Driver\={Microsoft Access Driver (*.mdb)};Dbq\=#LegacyDbPath#;DriverID\=22;READONLY\=true
Any thoughts?
try appending your url with
UID\=user;PWD\=pwd

Categories