Accessing a datasource from an OSGi bundle - spring

I have a blueprint file containing a datasource deployed to Apache ServiceMix. I was able to query the datasource from Apache Karaf console. How can I access this datasource from a Camel Spring-DM bundle application? This is my blueprint file:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
<property name="URL" value="URL"/>
<property name="user" value="USER"/>
<property name="password" value="PASSWORD"/>
</bean>
<service interface="javax.sql.DataSource" ref="dataSource" id="ds">
<service-properties>
<entry key="osgi.jndi.service.name" value="jdbc/ds"/>
</service-properties>
</service>
</blueprint>

You can bind the DataSource as an OSGi service. In spring dm this is osgi:reference, in blueprint it would be reference.
<reference id="dataSource" interface="javax.sql.DataSource"/>
You can then inject the DataSource for example into the SqlComponent.
As an example see a fix I did for this camel route. This is blueprint but it is almost the same for spring dm.
<bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="dataSource"/>
</bean>

Using Hibernate as JPA provide:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="jpa" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>osgi:service/jdbc/ds</jta-data-source>
...
</persistence-unit>
</persistence>

Related

Using JPA to connect oracle database,but it still shows Could not load requested class : oracle.jdbc.driver.OracleDriver

I use JPA configuration file,persistence.xml,to set the connection info below:
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="mydb" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.test.vo.Customer</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin://localhost:1521/orcl" />
<property name="javax.persistence.jdbc.user" value="cuser" />
<property name="javax.persistence.jdbc.password" value="cuser" />
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="none" />
</properties>
</persistence-unit>
And I have added ojdbc8.jar in Maven repository:
https://i.imgur.com/FGIlQxg.png
And I import ojdbc8.jar in Maven dependecy:
https://i.imgur.com/bkXg65L.png
And I also set ojdbc8.jar info in pom.xml:
https://i.imgur.com/koleE8Y.png
But when I try to connect to oracle database,it just shows
java.lang.ClassNotFoundException: Could not load requested class : oracle.jdbc.driver.OracleDriver
and I see the ojdbc8.jar in Maven dependecy ,the ojdbc8.jar surely contains oracle.jdbc.driver.OracleDriver
What happened about this?HOW Can I do to fix it?
Try Using
oracle.jdbc.OracleDriver
instead of
oracle.jdbc.driver.OracleDriver
This is what worked for me. Check your URL, which is not correct.
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
<property name="javax.persistence.jdbc.user" value="username"/>
<property name="javax.persistence.jdbc.password" value="password"/>
<property name="javax.persistence.jdbc.url" value=""jdbc:oracle:thin:#myhost:1521/myorcldbservicename"/>

Separate DB just for JUnit tests in Spring app

I am writing unit tests for my Spring application using in-file HSQL db. I don't want to pollute this db with test data, so I am trying to set up another in-memory HSQL db instance to be used in tests.
In my abstract testing class I import test application context XML configuration:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration("/testingContext.xml")
#Transactional
public abstract class AbstractDaoForTesting {
testingContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
<jdbc:embedded-database id="dataSource" type="HSQL" />
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.se.micom" />
<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.HSQLDialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf" />
</bean>
<tx:annotation-driven />
<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="characteristicDao" class="com.se.micom.dao.CharacteristicDaoImpl"/>
<bean id="functionalityDao" class="com.se.micom.dao.FunctionalityDaoImpl"/>
<bean id="segmentDao" class="com.se.micom.dao.SegmentDaoImpl"/>
<bean id="segmentService" class="com.se.micom.service.SegmentServiceImpl"/>
<bean id="functionalityService" class="com.se.micom.service.FunctionalityServiceImpl"/>
<bean id="characteristicService" class="com.se.micom.service.CharacteristicServiceImpl" />
</beans>
Up to now in-memory HSQL db is working fine in test.
However, as soon as I introduce in my project persistence.xml in src/main/resources/META-INF unit tests start to use this persistence unit and not the one from testingContext.xml.
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="micomPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.se.micom.dao.domain.Characteristic</class>
<class>com.se.micom.dao.domain.Functionality</class>
<class>com.se.micom.dao.domain.Segment</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:D:\workspace_java\Micom2\db\micom"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
</properties>
</persistence-unit>
</persistence>
What's wrong with this configuration? Why tests "prefer" to use db defined in persistence.xml?
Your problem is a perfect example for Spring profiles. It is always problematic to have multiple beans doing the same thing in your context at once if you want a certain one to be used.
You should have two different profiles, "prod" and "test", and wrap your bean declaration in the xml config accordingly with <beans profile="XYZ"> ... </beans>. A more extensive example can be found here.
You can then annotate your test with #Profile("test") and it should use the correct database.

Spring jpa(Hibernate) with tomcat

I'm working with JPA(Hibernate) and spring project these days, I configured spring, jpa and try to deploy the application on tomcat7, As I know jpa/hibernate creates tables using entity class first loading, but here I couldn't see any connection between database and application when applications starts, below you can see my configuration,I have seen lot of questions posted regarding this matter, I couldn't find the proper solution to my problem.
Can any one tell me what is the problem here.I have created a application without any issues in jetty server sometime back,
Thank you in advance
applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-lazy-init="true">
<!-- Use #Transaction annotations for managing transactions -->
<!--<tx:annotation-driven/>-->
<!-- holding properties for database connectivity / -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<tx:annotation-driven proxy-target-class="true" />
<!-- Activates scanning of #Autowired -->
<context:annotation-config/>
<!-- Activates scanning of #Repository -->
<context:component-scan base-package="lk.gov.elg"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"
p:dataSource-ref="dataSource"/>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:jpaVendorAdapter-ref="jpaAdapter">
<property name="persistenceUnitName" value="eLGPersistenceUnit"></property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>
</property>
</bean>
<!-- Jpa adapter for the Hibernate -->
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:databasePlatform="org.hibernate.dialect.MySQLDialect"
p:generateDdl="true"
p:showSql="true" >
</bean>
<bean
class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>
persistance.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="eLGPersistenceUnit"
transaction-type="RESOURCE_LOCAL">
<!--transaction-type="JTA">-->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
</properties>
<!--<provider>org.hibernate.ejb.HibernatePersistence</provider>-->
</persistence-unit>
</persistence>
I think there is no issue with your configuration, try after removing "default-lazy-init=true" attribute
Cheers
try changing:
<property name="hibernate.hbm2ddl.auto" value="validate"/>
to:
<property name="hibernate.hbm2ddl.auto" value="update"/>
or:
<property name="hibernate.hbm2ddl.auto" value="create"/>

Spring and Hibernate - changing dialect

In our web app that uses Spring and Hibernate, the hibernate configuration is in the META-INF/persistence.xml, but there is one problem, we are using two different databases, one for testing and other one for production.
Here is our `persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="SpringMVCTest" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/comp/env/jdbc/sqliteDS</jta-data-source>
<class>pl.meble.taboret.model.UserEntity</class>
<class>pl.meble.taboret.model.WordList</class>
<class>pl.meble.taboret.model.WordUnit</class>
<class>pl.meble.taboret.model.ActivateUserAccountPermaLink</class>
<class>pl.meble.taboret.model.ResetPasswordPermaLink</class>
<class>pl.meble.taboret.question.QuestionUnit</class>
<class>pl.meble.taboret.question.OpenQuestion</class>
<class>pl.meble.taboret.question.MultipleChoiceQuestion</class>
<class>pl.meble.taboret.question.WithGapsQuestion</class>
<class>pl.meble.taboret.question.QuestionList</class>
<class>pl.meble.taboret.answer.AnswerUnit</class>
<class>pl.meble.taboret.answer.OpenQuestionAnswer</class>
<class>pl.meble.taboret.answer.MultipleChoiceQuestionAnswer</class>
<class>pl.meble.taboret.answer.WithGapsQuestionAnswer</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="pl.meble.taboret.utils.SQLiteDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="true" />
<!-- <property name="hibernate.connection.driver_class" value="${database.driver}"
/> <property name="hibernate.connection.url" value="${database.url}" /> -->
<!-- <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/> -->
<!--<property name="hibernate.transaction.factory_class" value="com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory"/> -->
<property name="hibernate.transaction.manager_lookup_class" value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup"/>
</properties>
</persistence-unit>
</persistence>
so, is it possible to change the value of hibernate.properties at runtime, or store this value for example in JNDI resource?
Or is there some other way to conditionally set hibernate.dialect, so for example for testing we would have SQLite dialect and for normal deploy he would use Postgre dialect.
Yes. In spring you define the entity manager with a bean:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
You can configure properties of that bean:
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
where ${hibernate.dialect} is spring property. So you can pass the property when starting your project (either via -Dhibernate.dialect, or by placing it in a properties file and loading it with <context:property-placeholder-configurer>

spring-simple-memcache - maven dependencies for ReadThroughSingleCache

Has anyone used spring simple memcached? I have not been able to get the exact maven dependency and the repository where this is available.
The dependencies mentioned on the code.google page (http://code.google.com/p/simple-spring-memcached/) mention other dependencies but it does not include jar for Simple-spring-memcache itself.
Thanks for the help guys.
Try this one:
<dependency>
<groupId>com.google.code.simple-spring-memcached</groupId>
<artifactId>simple-spring-memcached</artifactId>
<version>2.0.0</version>
</dependency>
In case of SSM 2.0.0 you need also memcached client (xmemcached or spymemcached):
<dependency>
<groupId>com.google.code.simple-spring-memcached</groupId>
<artifactId>simple-spring-memcached</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>1.3.5</version>
</dependency>
and configure connection to local memcached server:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<import resource="simplesm-context.xml" />
<aop:aspectj-autoproxy />
<bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory">
<property name="cacheClientFactory">
<bean class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
</property>
<property name="addressProvider">
<bean class="com.google.code.ssm.config.DefaultAddressProvider">
<property name="address" value="127.0.0.1:11211" />
</bean>
</property>
<property name="configuration">
<bean class="com.google.code.ssm.providers.CacheConfiguration">
<property name="consistentHashing" value="true" />
</bean>
</property>
</bean>
</beans>

Resources