DB2 Datasource configuration - CXF, JBoss 6, Spring - spring

Currently, I have a data source declaration in my CXF.xml.
<bean id="myDB2DataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" />
<property name="url" value="<my db2 url>" />
<property name="username" value="<my username>" />
<property name="password" value="<my password>" />
<property name="connectionProperties">
<props>
<prop key="blockingReadConnectionTimeout">20</prop>
</props>
</property>
</bean>
This works well. But I need to move this to my datasource xml file (my-ds.xml) using probably JNDI.
something like:
(in cxf.xml)
<bean id="myDB2DataSource" class="org.springframework.jndi.JndiObjectFactoryBean"
scope="singleton">
<property name="jndiName">
<value>java:/jdbc.datasource.myDB2DataSource</value>
</property>
</bean>
and (in my-ds.xml):
<datasource jndi-name="java:/jdbc.datasource.myDB2DataSource"
pool-name="jdbc.datasource.myDB2DataSource" jta="false">
<connection-url>"<my db2 url>"
</connection-url>
<driver>com.ibm.db2.jcc.DB2Driver</driver>
<security>
<user-name><my username></user-name>
<password><my password></password>
</security>
</datasource>
But I can't make it work. What should be the correct config in cxf.xml and my-ds.xml?
I always encounter BeanCreationNotAllowedException. : Error creating bean with name 'cxf'
And many other exceptions which I believe caused by this since my bean myDB2DataSource can't be found ().
No matching bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Qualifier(value=myDB2DataSource)}
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceMIMS' defined in ServletContext resource [/WEB-INF/cxf.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: jdbc.datasource.myDB2DataSource -- service jboss.naming.context.java."jdbc.datasource.myDB2DataSource"
Thanks!

Related

I'm getting error while integrating spring and hibernate

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentManager' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'studentDao' while setting bean property 'studentDao';
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentDao' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory';
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Initialization of bean failed;
nested exception is java.lang.NoClassDefFoundError: javax/transaction/TransactionManager
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
default-lazy-init="true">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/Java2all"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>Student.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
<bean id="studentDao" class="com.dao.impl.StudentDaoHibernate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="studentManager" class="com.manager.StudentManagerImpl">
<property name="studentDao" ref="studentDao" />
</bean>
</beans>
student.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name ="com.springhibnernate.Student" table="Student">
<id name="id" type="java.lang.Long">
<generator class="native"/>
</id>
<property name="name" type="string">
<column name="name" length="40"/>
</property>
</class>
</hibernate-mapping>
There is a good chance you are missing "JTA-X.X.jar" in your classpath.

Spring data 1.6.0 GA cannot find entity manager factory bean by custom id

I have found the strange behavior of Spring data 1.6.0 (downgraded version 1.5.2 does not have this problem). It seems that this version strictly demands the id of entity manager factory bean to be "entityManagerFactory". If not, this error appears when running TestNG test:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMapppingContext': Cannot create inner bean '(inner bean)#36b87404' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#36b87404': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
My configuration is:
<!-- ************************************************************** -->
<!-- Database configuration -->
<!-- ************************************************************** -->
<!-- Entity manager factory bean -->
<bean id="entityManagerFactoryCustomId"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="test-system" />
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<array>
<value>${pds.db.scan.model}</value>
</array>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">${jdbc.showsql}</prop>
<prop key="hibernate.format_sql">${jdb.formatsql}</prop>
<prop key="hibernate.hbm2ddl.auto">${jdbc.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<!-- C3P0 connection pool -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- Connection properties -->
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Pool properties -->
<property name="minPoolSize" value="${pool.minsize}" />
<property name="maxPoolSize" value="${pool.maxsize}" />
<property name="initialPoolSize" value="${pool.initialPoolSize}" />
<property name="maxStatements" value="${pool.maxstatements}" />
<property name="acquireIncrement" value="${pool.acquireincrement}" />
<property name="preferredTestQuery" value="${jdbc.check}" />
<property name="numHelperThreads" value="${pool.threads}" />
</bean>
<!-- JPA transaction manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryCustomId" />
</bean>
<!-- Activate Spring Data JPA repository support -->
<jpa:repositories base-package="pds.archiva.db.repository" />
</code>
Is it a bug in Spring Data JPA or am I doing something wrong ? Tested on windows java 64bit 7u55 with following versions:
spring.framework.version = 4.0.5.RELEASE
spring.security.version = 3.2.4.RELEASE
spring.data-jpa.version = 1.6.0.RELEASE
As I have written, the same test works with only changing spring.data-jpa.version = 1.5.2.RELEASE ... or change id of bean to "entityManagerFactory" instead of "entityManagerFactoryCustomId".
Just added #StéphaneNicoll answer:
set the entity manager explicitly then (see entity-manager-factory-refelement in jpa:repositories . Looks like the documentation might be outdated.

CAS Server Configuration

I am implementing CAS server without using maven, have configured properly here is my deployerConfigContext.xml configuration:
<!-- This is the EntityManagerFactory configuration for Hibernate -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="showSql" value="true" />
</bean>
</property>
<property name="jpaProperties">
<props>
<!-- <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>-->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/testing"
p:password="***" p:username="root" />
now when I am trying to start the CAS server, it is throwing following exception:-
2014-02-12 18:49:06,664 ERROR [org.springframework.web.context.ContextLoader] - <Context initialization failed>
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servicesManager' defined in ServletContext resource [/WEB-INF/spring-configuration/applicationContext.xml]: Cannot resolve reference to bean 'serviceRegistryDao' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceRegistryDao' defined in ServletContext resource [/WEB-INF/deployerConfigContext.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/deployerConfigContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/deployerConfigContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool/ObjectPool
Please help;
Looks like the commons-pool-x.jar dependency is missing...

org.springframework.beans.factory.BeanCreationException: Error while mapping Spring with context datasource

This is my spring context file
<!-- <context:property-placeholder location="classpath:jdbc.properties" />-->
<context:component-scan base-package="com.max.premcalc" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/inquizzitiveds"/>
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean> -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.max.premcalc.domain.PremiumCalc</value>
<value>com.max.premcalc.domain.Inputparam</value>
<value>com.max.premcalc.domain.RiderInput</value>
<value>com.max.premcalc.domain.Rider</value>
<value>com.max.premcalc.domain.Product</value>
<value>com.max.premcalc.domain.Products</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="MainController" class="com.max.premcalc.controller.MainController" />
<bean id = "calcDao" class="com.max.premcalc.dao.CalcDaoImpl"/>
<bean id = "calcService" class="com.max.premcalc.service.CalcServiceImpl"/>
<bean id = "inputparam" class = "com.max.premcalc.domain.Inputparam"/>
But I'm getting this error :
Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private
com.max.premcalc.service.CalcService com.max.premcalc.controller.MainController.calcService; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'calcServiceImpl': Injection
of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: com.max.premcalc.dao.CalcDao com.max.premcalc.service.CalcServiceImpl.calcDao; nested
exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao':
Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void
com.max.premcalc.dao.CalcDaoImpl.setSessionFactory(org.hibernate.SessionFactory); nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in
ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is
org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
You need to set the property
hibernate.dialect
In the hibernate (persistence.xml or bean declaration) configuration, the value depends on your database, for example:
Postgres: org.hibernate.dialect.PostgreSQL82Dialect
Oracle: org.hibernate.dialect.Oracle10gDialect
All posible options are listen here
For example, a sample persistence.xml looks like:
<persistence-unit>
...
<properties>
...
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
...
</properties>
</persistence-unit>
Please, check if your properties file have the key hibernate-dialect
The stacktrace is pretty clear
org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
Apparently hibernate cannot use the metadata to retrieve the type of database to use, as a result one must configure the hibernate dialect explicitly.

Nothing Inserted while using Mysql and Neo4j in the same maven-web project

We are working on a maven-web project.In this project two databases used, MySQL and Neo4j.
Project is running fine with MySQL(without Neo4j). After adding Neo4j config file(neo4j-config.xml) project build without any error, insert operation to Mysql return successfully but actually nothing inserted to the tables.
With some research about this problem found information about "cross store operation":
XML configuration with cross-store
<context:annotation-config/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
</bean>
<datagraph:config graphDatabaseService="graphDatabaseService"
entityManagerFactory="entityManagerFactory"/>
<bean id="graphDatabaseService"
class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
<constructor-arg index="0" value="http://localhost:7474/db/data" />
</bean>
Our project db-context.xml
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="database" value="MYSQL" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="persistenceUnit" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>
Our Project neo4j-context.xml
<neo4j:config graphDatabaseService="graphDatabaseService" />
<bean id="graphDatabaseService"
class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
<constructor-arg index="0" value="http://localhost:7474/db/data" />
</bean>
After adding entityManagerFactory="entityManagerFactory" to our neo4j-config.xml file
we got an error like below while running maven project:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating b ean with name
'org.springframework.data.neo4j.config.Neo4jConfiguration#0': Error se
tting property values; nested exception is
org.springframework.beans.NotWritableProp ertyException: Invalid
property 'entityManagerFactory' of bean class [org.springfram
ework.data.neo4j.config.Neo4jConfiguration$$EnhancerByCGLIB$$5a2edf0]:
Bean property 'entityManagerFactory' is not writable or has an
invalid setter method. Does the pa rameter type of the setter match
the return type of the getter?
if we use datagraph tag instead of neo4j tag, i got an error like below
Offending resource: ServletContext resource [/WEB-INF/spring-servlet.xml]; nested ex ception is
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
Li ne 32 in XML document from ServletContext resource
[/WEB-INF/neo4j-config.xml] is in valid; nested exception is
org.xml.sax.SAXParseException; systemId:
http://www.springframework.org/schema/data/graph/datagraph-1.0.xsd;
lineNumber: 32; columnNumber: 51 ; src-resolve: Cannot resolve the
name 'repository:repository' to a(n) 'type definit ion' component.
Our problem is that although insert operation line returns success in debug mode, it does not add new record to table after adding neo4j config file to our project. We did not understand why this situation occurs with only adding neo4j-config.xml file. Second confusion is why adding "entityManagerFactory" does not solve this problem.
We couldn't find any example of Neo4j working with MySQL in maven-web project except spring-data example (myrestaurants-social) .
Thank you for all valuable responses in advance.

Resources