Announcement Announcement Module
Collapse

Spring Dynamic Modules forum decommissioned in favor of Eclipse Gemini Blueprint

With the official first release of Eclipse Gemini Blueprint shipped, the migration of the Spring Dynamic Modules code base to the Eclipse Foundation, as part of the Gemini project, has been completed.

As such, this forum has been decommissioned in favour of the Eclipse Gemini forums.
See more
See less
Hibernate in OSGI Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate in OSGI

    Everyone knows we can get the transaction management when integrated hibernate with spring. We declare the dao class in the xml file of spring, it can works well. But it occurs error when using spring DM. I think it maybe due to the classloader which is special in osgi.

    I made a osgi bundle which contains the common hibernate datasource declaration xml file, the content are:
    Code:
    	<bean id="dataSource"
          	class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName">
    			<value>com.mysql.jdbc.Driver</value>
    		</property>
    		<property name="url">
    			<value>jdbc:mysql://127.0.0.1:3306/auditsystem</value>
    		</property>
    		<property name="username">
    			<value>audituser</value>
    		</property>
    		<property name="password">
    			<value>123</value>
    		</property>
    	</bean>
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.AnnotationSessionFactoryBean">
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">
    					org.hibernate.dialect.SQLServerDialect
    				</prop>
    				<prop key="hibernate.show_sql">true</prop>
    				<prop key="hibernate.cglib.use_reflection_optimizer">
    					true
    				</prop>
    				<prop key="hibernate.cache.provider_class">
    					org.hibernate.cache.HashtableCacheProvider
    				</prop>
    				<prop key="hibernate.hbm2ddl.auto">create</prop>
    			</props>
    		</property>
    
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    	</bean>
    
    	<!-- Spring Data Access Exception Translator Defintion -->
    	<bean id="jdbcExceptionTranslator"
    		class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    	</bean>
    
    	<!-- Hibernate Transaction Manager Definition -->
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref local="sessionFactory" />
    		</property>
    	</bean>
    	
    	<!-- Hibernate Template Defintion -->
    	<bean id="hibernateTemplate"
    		class="org.springframework.orm.hibernate3.HibernateTemplate">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    		<property name="jdbcExceptionTranslator">
    			<ref bean="jdbcExceptionTranslator" />
    		</property>
    	</bean>
    I also publish the hibernateTemlate as a osgi service:
    Code:
    	<!-- Hibernate Template Defintion -->
    	<osgi:service ref="hibernateTemplate"
    		interface="org.springframework.orm.hibernate3.HibernateOperations">
    	</osgi:service>
    But it can't works properly at all, the error information are:
    Code:
    SpringOsgiExtenderThread-2 ERROR [org.springframework.osgi.context.support.OsgiBundleXmlApplicationContext] - Post refresh error
    org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in URL [bundleentry://33/META-INF/spring/appContext-datasource.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource not found from bundle [net.jianzi.eas.core]
    	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1138)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:524)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1174)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:754)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:422)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
    	at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:276)
    	at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:145)
    	at java.lang.Thread.run(Thread.java:619)
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource not found from bundle [net.jianzi.eas.core]
    	at org.springframework.osgi.util.BundleDelegatingClassLoader.findClass(BundleDelegatingClassLoader.java:103)
    	at org.springframework.osgi.util.BundleDelegatingClassLoader.loadClass(BundleDelegatingClassLoader.java:156)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    	at org.springframework.util.ClassUtils.forName(ClassUtils.java:242)
    	at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:383)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1135)
    	... 8 more
    I also added the import package statement in the above bundle, but it still error.

    Does anybody has the same problem?
    Last edited by growing; Aug 26th, 2008, 07:44 AM.

  • #2
    Growing, first please use [ c o d e ] as without them, your post is highly unreadable.
    The problem basically is that the commons dbcp classes are not visible and thus cannot be loaded by the Hibernate configuration.
    Add the proper imports for them to your bundle and you should be set.
    Additionally, if you are an OSGi newbie try one of the tutorials from the Spring-DM homepage and S2AP.

    Comment

    Working...
    X