I am new to spring framework. I am creating a connection pool using Hikari in spring MVC(version 4) + hibernate (version 4) framework application. I am getting the below error while running my application.
Invalid property 'url' of bean class [com.zaxxer.hikari.HikariConfig]: Bean property 'url' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
dispatcher-servelt.xml:
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
<!-- HikariConfig config that is fed to above dataSource -->
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="driverClassName" value="${postgres.driver}" />
<property name="url" value="${postgres.url}?stringtype=unspecified" />
<property name="username" value="${postgres.user}" />
<property name="password" value="${postgres.password}" />
<property name="maximumPoolSize" value="${hikari.maximumPoolSize}" />
<property name="idleTimeout" value="30000" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<array>
<value>XXXX</value>
<value>XXXX</value>
</array>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.provider_class">org.hibernate.hikaricp.internal.HikariCPConnectionProvider</prop>
<prop key="hibernate.dialect">${postgres.hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.order_inserts">true</prop>
<prop key="hibernate.order_updates">true</prop>
<prop key="hibernate.jdbc.batch_versioned_data">true</prop>
</props>
</property>
</bean>
hibernate.properties file:
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create/update
hibernate.connection.provider_class=com.zaxxer.hikari.hibernate.HikariConnectionProvider
hibernate.hikari.minimumIdle=5
hibernate.hikari.maximumPoolSize=10
hibernate.hikari.idleTimeout=25200
hibernate.hikari.connectionTimeout=25200
hibernate.hikari.dataSourceClassName=org.postgresql.Driver
hibernate.hikari.dataSource.url=XXXX
hibernate.hikari.dataSource.user=XXXX
hibernate.hikari.dataSource.password=XXXX
hibernate.CharSet=true
hibernate.characterEncoding=true
hibernate.useUnicode=true
pom.xml:
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
java version: 8, tomcat: 8.5
Related
I'm working with Hibernate. How can I configure my applicationContext.xml to have an H2 in-memory databaseorg.hibernate.dialect.H2Dialect dont work
Spring configuration
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.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:tcp://localhost/~/test" />
<property name="username" value="sa" />
<property name="password " value="" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.emusicstore</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
You are not connecting to an in-memory database. Your JDBC URL is for network connection to localhost:
jdbc:h2:tcp://localhost/~/test
To use the in-memory H2 the URL must look like this, containing mem:
jdbc:h2:mem:testdb
In the manual, see the section on In-Memory Database
Use embedded db datasource (with dbcp connection pool) instead of drivermanager datasource.
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:db/sql/create-db.sql" />
<jdbc:script location="classpath:db/sql/insert-data.sql" />
</jdbc:embedded-database>
<bean id="dbcpDataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:hsqldb:mem:dataSource" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="dbcpDataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.emusicstore</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
I am miagrating my database from MySql to H2 and I keep getting the error message
org.h2.jdbc.JdbcSQLException: Table "DEVICE" not found
Everything was mapped correctly and worked with MySql. I only changed the context.xml file to work with H2 and added a dependency for H2 in the Pom.xml file.
context.xml file:
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:~/dataStore2"/>
<property name="username" value="" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.entities" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="format_sql">true</prop>
<prop key="use_sql_comments">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
The Device class:
package com.entities;
#Entity
#Table(name="DEVICE")
public class Device {
...
}
You're missing
<prop key="hibernate.hbm2ddl.auto">create</prop>
in
<property name="hibernateProperties">
to force Hibernate to create schema based on entity classes if it is missing. You also need to change dialect from MySQL to H2:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.entities" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="format_sql">true</prop>
<prop key="use_sql_comments">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
Reference: Hibernate, Chapter 3. Configuration, Table 3.7. Miscellaneous Properties
I have Spring MVC application divided on layers. One of them is Service and here's one of the methods
#Transactional
public boolean deleteProject(long id){
Project project = projectDAO.read(id);
taskDAO.deleteAllByProject(project);
projectDAO.delete(project);
return true;
}
It deletes tasks, but project remains in the db (with no error or exception). I didnt use #Transactional in repository classes.
Here's my Spring config
<?xml version="1.0" encoding="UTF-8"?>
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/automate" />
<property name="username" value="postgres" />
<property name="password" value="" />
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="ru.jeak.keep" />
<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">update</prop>
<prop key="connection.pool_size">3</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
<prop key="hibernate.default_schema">test</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<bean id="userHibernateDAO" class="ru.jeak.keep.repository.hibernate.UserHibernateDAO" />
<bean id="projectHibernateDAO" class="ru.jeak.keep.repository.hibernate.ProjectHibernateDAO" />
<bean id="taskHibernateDAO" class="ru.jeak.keep.repository.hibernate.TaskHibernateDAO" />
UPDATE
I added persistence #Transactional to DAO class and it works now. But Im still confused why Spring did not do the rollback of deleting tasks after failure of deleting project
I am getting the below exception when in my application. I am using Spring and Hibernate and the dao layer is mostly auto generated by myeclipse.
nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:616)
The configurations are as below. The auto generated code had bean id's as dataSource, sessionFactory and hibernateTemplate and it was working fine. Then I changed the bean ids to tseDataSource, tseSessionFactory and tseHibernateTemplate. I updated the hibernateTemplate parameter passed to the DAO's accordingly. Then I started getting the above error. When I change these back to dataSource, sessionFactory and hibernateTemplate, the app works fine. But I had to add another datasource, So i have to change these. Is there any reason why this error happens on changing these bean ids.
<bean id="tseDataSource" autowire="byName"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://10.4.4.4:3306/tse">
</property>
<property name="username" value="tse"></property>
<property name="password" value="tse"></property>
</bean>
<bean id="tseSessionFactory" autowire="byName"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="tseDataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>Conf/hibernate/tse</value>
</list>
</property>
</bean>
<bean id="tseHibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate" autowire="byName" >
<property name="sessionFactory">
<ref local="tseSessionFactory" />
</property>
</bean>
<bean id="tfDataSource" autowire="byName"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://10.4.4.4:3306/tf">
</property>
<property name="username" value="tf"></property>
<property name="password" value="tf"></property>
</bean>
<bean id="tfSessionFactory" autowire="byName"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="tfDataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<value>Conf/hibernate/tf</value>
</property>
</bean>
<bean id="tfHibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate" autowire="byName" >
<property name="sessionFactory">
<ref local="tfSessionFactory" />
</property>
</bean>
<bean id="namesDAO" class="com.NamesDAO" autowire = "byName" >
<property name="hibernateTemplate" ref="tfHibernateTemplate"/>
</bean>
<bean id="LogDAO"
class="com.LogDAO"
autowire="byName">
<property name="hibernateTemplate" ref="tseHibernateTemplate" />
</bean>
I want to define a routingdatasource for two different databases: mysql and oracle
I've defined the following config.xml
<bean id="dataSourceTarget" class="com.test.common.RoutingDataSource">
<property name="targetDataSources">
<map key-type="com.test.common.DataSourceType">
<entry key="MY_SQL" value-ref="mySqlDataSource"/>
<entry key="ORACLE" value-ref="oracleDataSource"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="mySqlDataSource"/>
</bean>
<bean id="mySqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driver.classname}" />
<property name="jdbcUrl" value="${mysql.url}" />
<property name="user" value="${mysql.username}" />
<property name="password" value="${mysql.password}" />
</bean>
<bean id="oracleDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
<property name="jdbcUrl" value="${oracle.url}" />
<property name="user" value="${oracle.user}" />
<property name="password" value="${oracle.passw}" />
</bean>
and now I can define session factory and transaction manager
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
</props>
</property>
</bean>
But this is doesn't work. I'm getting the
Caused by: java.lang.UnsupportedOperationException: The user must supply a JDBC connection
And in addition I must define the dialect for session.
But how can I define it depending on the datasourse it is working on?