I am trying to use Hikari connection pooling for my spring based app using xml based bean configuration. Below is my Hikari config bean and I am using the Db2 as my database.
<bean id="HikariConfig_UId_Primary" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="uidPrimaryCP" />
<property name="connectionTestQuery" value="select 1 from sysibm.systables fetch first row only with UR" />
<property name="dataSourceClassName" value="${Jdbc_DataSourceClassName}" />
<property name="maximumPoolSize" value="${Jdbc_MaxPoolSize}"/>
<property name="idleTimeout" value="${Jdbc_IdleTimeOut}" />
<property name="maxLifetime" value="${Jdbc_MaxLifeTime}" />
<property name="connectionTimeout" value="${Jdbc_ConnTimeOut}" />
<property name="dataSourceProperties">
<props>
<prop key="url">${Jdbc_UID_Primary}</prop>
<prop key="user">${Jdbc_UserId}</prop>
<prop key="password">${Jdbc_Password}</prop>
</props>
</property>
</bean>
<bean id="UID_Primary_DataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="HikariConfig_UId_Primary" />
</bean>
It failing to register the bean, saying the database url property - url does not exists on target class com.ibm.db2.jcc.DB2SimpleDataSource.
below is the complete stack trace..
com.ibm.ws.webcontainer.webapp.WebApp notifyServletContextCreated SRVE0283E: Exception caught while initializing context: {0}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UID_Primary_DataSource' defined in class path resource [config/SpringDbContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.zaxxer.hikari.HikariDataSource]: Constructor threw exception; nested exception is java.lang.RuntimeException: Property url does not exist on target class com.ibm.db2.jcc.DB2SimpleDataSource
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:278)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1114)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1017)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.deere.u90.iafservice.unifieduser.application.ApplicationRegistry.loadSpringLdapContext(ApplicationRegistry.java:
any help is greatly appreciated..
thanks..
I've connected to Hikari using spring boot but not with xml configuration. See my post here
I think, adding up below property should fix error.
<property name="jdbcUrl" value="" />
<property name="username" value="" />
<property name="password" value="" />
You seem to have several things wrong. You didn't post the value of variable ${Jdbc_DataSourceClassName} be it appears to resolve to DB2SimpleDataSource at runtime. Per this IBM KnowledgeCenter topic, that datasource is not poolable and since you started the post with "I am trying to use Hikari connection pooling", I'm assuming you want a poolable DS like com.ibm.db2.jcc.DB2ConnectionPoolDataSource as described here.
The second problem is that the DB2 Datasource classes don't support a url property, only the DB2 DriverManager interface supports it. The KC topic for the abstract base of the DB2 data source classes, DB2BaseDataSource, says
You can set all properties on a DataSource or in the url parameter in
a DriverManager.getConnection call.
url is not one of the listed properties, so you're going to need to establish the connection to the dataasource using the hostname, port, etc properties
Related
So I have a project I want to start work with. The senior developer sent me a zip package of the project and database.sql file for initializing the local database for me to just launch the project. But we are facing the error.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/spring/spring-database.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
All entities are exactly the same as in the local database. I have changed the XML file config to match my password and URL. Spend 2 days trying to run the project. Will appreciate any help. Please feel free to ask any questions.
<!-- Configure the entity manager factory bean -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<!-- Set JPA properties -->
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.ProgressDialect</prop>
</props>
</property>
<property name="packagesToScan" value="nl.impressie.grazer.hibernate" />
</bean>
Sounds like hibernate is failing to initialize due to a failure to connect to the DB.
Are you sure you have a Postgresql DB running? Is Spring configured correctly to interface with that DB? (i.e. is the host, port, databaseschema, username and password set?)
If this is a new project, you shouldn't be using any of this XML configuration at all. Instead, create a template project at https://start.spring.io that includes the JPA starter, set your spring.datasource.url, and be running in 2 minutes.
I have a Spring application, which is configured for multiple Persistent units.
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list value-type="java.lang.String">
<value>classpath*:META-INF/*-persistence.xml</value>
</list>
</property>
<property name="dataSources">
<map>
<entry key="dataSource" value-ref="dataSource" />
</map>
</property>
<property name="defaultDataSource" ref="dataSource" />
<property name="defaultPersistenceUnitName" value="pm-model" />
</bean>
However, I'm getting an error on startup saying that one of the persistent units is not able to load. I also called out in the code above to list all the persistence.xml files, but still getting the same error.
ERROR 2017-08-10 15:33:44,882 [org.springframework.web.context.ContextLoader] - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bService': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com-model' is defined
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:307)
If anyone has any suggestions, I'm all ears. Thanks in advance.
I am having issues with setting up multi-tenancy support for my web application with a separate database approach using Spring + JPA + Hibernate.
I have tried the Hibernate way using my own implementations of CurrentTenantIdentifierResolver and AbstractMultiTenantConnectionProvider, and the Spring one using AbstractRoutingDataSource. I'll use the AbstractRoutingDataSource solution, that I named UserRoutingDataSource, to explain my problem.
What I am trying to achieve is the following :
I have a "master" database with all the data concerning the users and their databases
When a user logs in I save his database id in a session scoped bean named UserSession
My UserRoutingDataSource's targetDataSources needs to be filled with datasources representing all the databases of my master database's CustomDatabases table.
When a connection is needed, my UserRoutingDataSource's determineCurrentLookupKey needs to retrieve the database id from the user's UserSession instance.
In both cases (Hibernate way / Spring way) I end up with a circular reference exception. The issue arises when I try to autowire my CustomDatabasesDAO and my UserSession in UserRoutingDataSource.
This is the exception I get :
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userEntityManagerFactory' defined in class path resource [MyApp/webapp/WEB-INF/config/applicationContext.xml]: Cannot resolve reference to bean 'userRoutingDataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userRoutingDataSource' defined in class path resource [MyApp/webapp/WEB-INF/config/applicationContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [myApp.java.data.dao.Global.CustomDatabasesDAO]: : Error creating bean with name 'customDatabasesDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'userEntityManagerFactory': FactoryBean which is currently in creation returned null from getObject; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customDatabasesDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'userEntityManagerFactory': FactoryBean which is currently in creation returned null from getObject
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1051)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:828)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4728)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5162)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userRoutingDataSource' defined in class path resource [MyApp/webapp/WEB-INF/config/applicationContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [myApp.java.data.dao.Global.CustomDatabasesDAO]: : Error creating bean with name 'customDatabasesDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'userEntityManagerFactory': FactoryBean which is currently in creation returned null from getObject; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customDatabasesDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'userEntityManagerFactory': FactoryBean which is currently in creation returned null from getObject
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
... 24 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customDatabasesDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'userEntityManagerFactory': FactoryBean which is currently in creation returned null from getObject
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:357)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 34 more
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'userEntityManagerFactory': FactoryBean which is currently in creation returned null from getObject
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:181)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:127)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1584)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:253)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.orm.jpa.EntityManagerFactoryUtils.findEntityManagerFactory(EntityManagerFactoryUtils.java:130)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findNamedEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:556)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:538)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:707)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:680)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:354)
... 46 more
What I don't understand is why I get this exception when my CustomDatabasesDAO doesn't have a single reference to userEntityManagerFactory. The only relevant thing that I can think of in CustomDatabasesDAO is my call to the master database's entityManager :
#PersistenceContext(unitName = "masterEntityManagerFactory")
private EntityManager masterEntityManager;
For more context, here are my UserRoutingDataSource file and the relevant parts from my application context file.
UserRoutingDataSource
public class UserRoutingDataSource extends AbstractRoutingDataSource {
/*private CustomDatabasesDAO customDatabasesDAO;
#Autowired
public void setCustomDatabasesDAO(final CustomDatabasesDAO customDatabasesDAO)
{
this.customDatabasesDAO = customDatabasesDAO;
}*/
#Autowired
private UserSession session;
/*#Autowired
public void setUserSession(final UserSession session)
{
this.session = session;
}*/
#Autowired
public UserRoutingDataSource(CustomDatabasesDAO customDatabasesDAO) {
Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
for(CustomDatabases database : customDatabasesDAO.findDatabasesByDeleted(0))
{
// All the information necessary for the datasource will eventually be retrieved from the database variable
DriverManagerDataSource datasource = new DriverManagerDataSource();
datasource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
datasource.setUrl("jdbc:sqlserver://localhost:1433;databaseName=" + database.getCdboAliasName() + ";");
datasource.setUsername("username");
datasource.setPassword("password");
targetDataSources.put(String.valueOf(database.getCdboDatabaseId()), datasource);
}
setTargetDataSources(targetDataSources);
}
#Override
protected Object determineCurrentLookupKey() {
return session.getCdboDatabaseId();
}
}
Application context
/*<!--<bean id="currentTenantIdentifierResolverImpl" class="myApp.java.config.CurrentTenantIdentifierResolverImpl" />
<bean id="multiTenantConnectionProvider" class="myApp.java.config.MultiTenantConnectionProvider" />-->*/
<bean id="masterDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=myDatabase;" />
<property name="username" value="sa" />
<property name="password" value="password" />
</bean>
<bean id="masterEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="masterDataSource" />
<property name="packagesToScan" value="myApp.java.data.model" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.use_outer_join">true</prop>
</props>
</property>
</bean>
<bean id="userRoutingDataSource" class="myApp.java.config.UserRoutingDataSource">
<property name="targetDataSources">
<map />
</property>
</bean>
<bean id="userEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="userRoutingDataSource" />
<property name="packagesToScan" value="myApp.java.data.model" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
<entry key="hibernate.show_sql" value="false" />
<entry key="hibernate.use_outer_join" value="true" />
/*<!--<entry key="hibernate.tenant_identifier_resolver" value-ref="currentTenantIdentifierResolverImpl" />
<entry key="hibernate.multi_tenant_connection_provider" value-ref="multiTenantConnectionProvider" />
<entry key="hibernate.multiTenancy" value="DATABASE" />-->*/
</map>
</property>
</bean>
<!-- Transaction managers -->
<tx:annotation-driven />
<bean id="masterTransactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="masterEntityManagerFactory" />
<qualifier value="master" />
</bean>
<bean id="userTransactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="userEntityManagerFactory" />
<qualifier value="user" />
</bean>
I have tried so many things that I am not sure if I still understand how dependency injection works. And I have no idea what I might be missing.
Thanks for you help.
SOLUTION :
I solved my issue thanks to Roman's answer. I also gave up on the idea of using my session scoped bean to determine my current lookup key and I use my Spring Security authentication instead. When a user logs in I do the following :
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
UsernamePasswordAuthenticationToken newAuth = new UsernamePasswordAuthenticationToken(auth.getPrincipal(), auth.getCredentials(), authorities);
HashMap<String, Object> details = new HashMap<String, Object>();
details.put("databaseId", session.getCdboDatabaseId());
newAuth.setDetails(details);
SecurityContextHolder.getContext().setAuthentication(newAuth);
The important part being the setDetails(details) on my user's UsernamePasswordAuthenticationToken.
Here is the current state of my (working) configuration :
UserRoutingDataSource
#Component
public class UserRoutingDataSource extends AbstractRoutingDataSource {
#Autowired
public UserRoutingDataSource(CustomDatabasesDAO customDatabasesDAO) {
Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
for(CustomDatabases database : customDatabasesDAO.findDatabasesByDeleted(0))
{
DriverManagerDataSource datasource = new DriverManagerDataSource();
datasource.setDriverClassName(database.getCdboDriverName());
datasource.setUrl("jdbc:sqlserver://"+database.getCdboServer()+":"+database.getCdboPort()+";databaseName="+database.getCdboAliasName() + ";");
datasource.setUsername(database.getCdboUserName());
datasource.setPassword(database.getCdboPassword());
targetDataSources.put(database.getCdboDatabaseId(), datasource);
}
/*
* This default datasource is necessary because for some reason (Hibernate, JPA related ?) the routing datasource
* calls the "determineCurrentLookupKey()" on startup which returned null ("default" now) because there is no Authentication at startup yet.
*/
targetDataSources.put("default", new DriverManagerDataSource());
setTargetDataSources(targetDataSources);
afterPropertiesSet();
}
#Override
protected Object determineCurrentLookupKey() {
UsernamePasswordAuthenticationToken auth = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
if (auth != null && auth.getDetails() instanceof Map)
{
HashMap<String, Object> details = (HashMap<String, Object>) auth.getDetails();
return details.get("databaseId");
}
return "default";
}
}
Application context
<bean id="masterDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=myDatabase;" />
<property name="username" value="myUser" />
<property name="password" value="myPassword" />
</bean>
<bean id="masterEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="masterEntityManagerFactory" />
<property name="dataSource" ref="masterDataSource" />
<property name="packagesToScan" value="myApp.java.data.model" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.use_outer_join">true</prop>
</props>
</property>
</bean>
<bean id="userEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="userEntityManagerFactory" />
<property name="dataSource" ref="userRoutingDataSource" />
<property name="packagesToScan" value="myApp.java.data.model" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
<entry key="hibernate.show_sql" value="false" />
<entry key="hibernate.use_outer_join" value="true" />
</map>
</property>
</bean>
<!-- Transaction managers -->
<tx:annotation-driven />
<bean id="masterTransactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="masterEntityManagerFactory" />
<qualifier value="master" />
</bean>
<bean id="userTransactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="userEntityManagerFactory" />
<qualifier value="user" />
</bean>
Spring iterates over all EntityManagerFactorys inside of EntityManagerFactoryUtils.findEntityManagerFactory() to find out the factory with the property persistenceUnitName value equals to #PersistenceUnit's attribute unitName value. Only if no candidate has been found the bean with the name of unitName value will be used. Therefore userEntityManagerFactory gets instantiated during this process.
As a workaround you could try to make use of regular #Autowired instead of #PersistenceUnit in CustomDatabasesDAO class.
I am integrating Spring with Hibernate. However, when I run the application I get an error like:
Error creating bean with name 'sessionFactory' defined in class path resource [resources/spring.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to get the default Bean Validation factory
My spring.xml looks like:
<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-3.0.xsd">
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/3encult" />
<property name="password" value="3encult" />
<property name="username" value="3encult" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="mappingResources">
<list>
<value>
resources/User.hbm.xml
<!-- Project.hbm.xml ProjCF.hbm.xml Task.hbm.xml TaskCF.hbm.xml Category.hbm.xml
TaskEstimation.hbm.xml ProjectEstimation.hbm.xml Parameter.hbm.xml StatisticTool.hbm.xml
Report.hbm.xml -->
</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<!-- Especificamos el valor minimo del pool de conexiones -->
<prop key="hibernate.c3p0.minPoolSize">5</prop>
<!-- Especificamos el valor maximo del pool de conexiones -->
<prop key="hibernate.c3p0.maxPoolSize">20</prop>
<!-- El tiempo de vida de cada conexion del pool. -->
<prop key="hibernate.c3p0.timeout">600</prop>
</props>
</property>
</bean>
<bean id="myUserDAO" class="main.java.com.gwt.app.server.User">
<property name="sesionFactory" ref="sessionFactory" />
</bean>
</beans>
And the injection in class User is:
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"resources/spring.xml"});
public void setSesionFactory(SessionFactory sesionFactory){
this.sesionFactory = sesionFactory;
this.sesion = this.sesionFactory.openSession();
}
The stacktrace is:
Error creating bean with name 'sessionFactory' defined in class path resource [resources/spring.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to get the default Bean Validation factory
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [resources/spring.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to get the default Bean Validation factory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:900)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:455)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at main.java.com.gwt.app.server.User.<init>(User.java:16)
at main.java.com.gwt.app.server.GreetingServiceImpl.greetServer(GreetingServiceImpl.java:20)
at main.java.com.gwt.app.client.Main.main(Main.java:10)
Caused by: org.hibernate.HibernateException: Unable to get the default Bean Validation factory
at org.hibernate.cfg.beanvalidation.BeanValidationActivator.applyDDL(BeanValidationActivator.java:127)
at org.hibernate.cfg.Configuration.applyBeanValidationConstraintsOnDDL(Configuration.java:1677)
at org.hibernate.cfg.Configuration.applyConstraintsToDDL(Configuration.java:1627)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1418)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1348)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:717)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1479)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 14 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.cfg.beanvalidation.BeanValidationActivator.applyDDL(BeanValidationActivator.java:118)
... 22 more
Caused by: org.hibernate.HibernateException: Unable to build the default ValidatorFactory
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:383)
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.applyDDL(TypeSafeActivator.java:109)
... 27 more
Caused by: javax.validation.ValidationException: Unable to find a default provider
at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:264)
at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:380)
... 28 more
Build path contains the following libraries:
hibernate3.jar
org.springframework.aop-3.1.0.M1.jar
org.springframework.asm-3.1.0.M1.jar
org.springframework.aspects-3.1.0.M1.jar
org.springframework.beans-3.1.0.M1.jar
org.springframework.context-3.1.0.M1.jar
org.springframework.context.support-3.1.0.M1.jar
org.springframework.core-3.1.0.M1.jar
org.springframework.expression-3.1.0.M1.jar
org.springframework.instrument-3.1.0.M1.jar
org.springframework.instrument.tomcat-3.1.0.M1.jar
org.springframework.jdbc-3.1.0.M1.jar
org.springframework.jms-3.1.0.M1.jar
org.springframework.orm-3.1.0.M1.jar
org.springframework.oxm-3.1.0.M1.jar
org.springframework.transaction-3.1.0.M1.jar
I am not using validation. It is the reason of not include the validation jar.
I do not know what happens, I would like someone could help me.
Thanks!
Hibernate raises an exception: throw new HibernateException( "Unable to get the default Bean Validation factory", e); if it can not access Bean Validation Framework (BeanValidationActivator).
So I guess the problem is that the Validation jars are missing.
For Maven:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.0.2.GA</version>
</dependency>
Try adding following maven dependencies (or download the JAR's if you don't use Maven):
Hibernate Validator
Javax Validation
Other dependencies might also be required (consider to use Maven if you are not yet, to avoid issues like this)
Try once by adding this line to your hibernate.cfg.xml file
<property name="javax.persistence.validation.mode" value="none"/>
I have updated the Tomcat version from 8.5 to 9.0, it worked for me.
Since three days i am trying to get my Spring-Hibernate programs run. i had really tough time finding the involved dependencies due to version difference between hibernate2 and hibernate3.
Finally i was able to run program with following set of dependencies
cglib-nodep-2.1_3.jar
commons-collections.jar
commons-dbcp.jar
commons-pool.jar
commons-logging.jar
dom4j-1.4.jar
ehcache-1.6.0-beta1.jar
hibernate-3.1.3.jar
hsqldb.jar
jta.jar log4j-1.2.9.jar
mysql-connector-java-5.0.8-bin.jar
org.springframework.orm-3.1.0.M1.jar
org.springframework.transaction-3.1.0.M1.jar
spring-2.5.6.jar
spring-beans-2.0.4.jar
now after two days of efforts when i was able to manage the above mentioned dependencies i tried building similar program but it is throwing following error.I tried online for solution but the solution which i found there is not correct version of spring and hibernate ...Can any one tell me correct cause of exception and also correct version of the Spring and hibernate
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myRecordDAO' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'hibernateTemplate' while setting bean property 'hibernateTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' 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.reflect.MalformedParameterizedTypeException
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
I am also adding my application context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/subhash"/>
<property name="username" value="root"/>
<property name="password" value=""></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>MyRecord.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="myRecordDAO" class="com.shoesobjects.MyRecordDAOHibernateWithSpring">
<property name="hibernateTemplate"><ref local="hibernateTemplate"/></property>
</bean>
</beans>
a)
Note
As of Spring 3.0, Spring requires Hibernate 3.2 or later.
Source:
Spring 3.1.x Reference >
Hibernate
b)
org.springframework.orm-3.1.0.M1.jar
org.springframework.transaction-3.1.0.M1.jar
spring-2.5.6.jar
spring-beans-2.0.4.jar
Do you really think mixing current pre-release versions (3.1.x) with ancient versions (2.0.4 was released in 2007) is a good idea?
As matt says: use a dependency managements system like Maven, what you are dealing with is jar hell. Here's an article about referencing Spring 3.x artifacts from maven:
Obtaining Spring 3 Artifacts with
Maven
I suggest using a dependency management tool like Maven or Apache Ivy so you don't have to handle sorting through the dependencies and required versions on your own.
Hibernate docs on using with Maven
Got my problem resolved ...As pointed by Sean it was due to mixing different versions of spring and hibernate. Message for other newbies is please use latest version of spring and hibernate.As of Spring 3.0, Spring requires Hibernate 3.2 or later.one should never mix old and new version.
Here's a simple file that worked for me, I used transaction manager and not template.
<bean id="mySqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/stackoverflow" />
<property name="user" value="root" />
<property name="password" value="******" />
<property name="maxPoolSize" value="50" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="mySqlDataSource" />
<property name="mappingResources">
<list>
<value>Post.hbm.xml</value>
<value>Tag.hbm.xml</value>
<value>User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
</value>
<!--
hibernate.connection.provider_class =
org.hibernate.connection.C3P0ConnectionProvider
hibernate.hbm2ddl.auto=update
hibernate.current_session_context_class=thread
-->
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<!--
<property name="dataSource" ref="mySqlDataSource"/>
-->
</bean>
Also, I think the hibernate download package comes with separate folders that indicate what its specific dependencies are. As for the interrelation between hibernate and spring, I think you will have to use some dependency management tool as others have suggested. I do not think maven is a convenient tool. Just don't ever depart from maven's expected project structure and you should be fine.