The table "customer" is under the default schema "APP". When I run, I get an error:
java.sql.SQLSyntaxErrorException: Table/View 'CUSTOMER' does not exist.
The same jdbcUrl works through DriverManager.getConnection through.
I tried to include the schema name as part of jdbcUrl:
<property name="jdbcUrl" value="jdbc:derby:web_customer_tracker; user=app" />, but still no luck.
<!-- Step 1: Define Database DataSource / connection pool -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="jdbcUrl" value="jdbc:derby:web_customer_tracker" />
<!-- these are connection pool properties for C3P0 -->
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="30000" />
</bean>
<!-- Step 2: Setup Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="com.luv2code.springdemo.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
Related
I am trying to use c3pO connection pooling but getting below error:
Unknown system variable 'tx_isolation'
I am using below jars:
mysql-connector-java-8.0.11.jar
c3p0-0.9.5.3.jar
mchange-commons-java-0.2.15.jar
And below configuration:
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/customer_tracker?useSSL=false&serverTimezone=UTC" />
<property name="user" value="root" />
<property name="password" value="root" />
<!-- these are connection pool properties for C3P0 -->
<property name="minPoolSize" value="3" />
<property name="maxPoolSize" value="10" />
<property name="maxIdleTime" value="30000" />
</bean>
<!-- Step 2: Setup Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="com.springdemo.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
I am trying to map same embeddable object to my entity class.
#Entity
public class Person {
#Embedded
public Address home;
#Embedded
public Address work;
}
#Embeddable
public class Address {
public String street;
public String poBox;
}
and below is my configuration file
<?xml version="1.0" encoding="UTF-8"?>
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- access -->
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/portfolio" />
<property name="user" value="root" />
<property name="password" value="root" />
<!-- pool sizing -->
<property name="initialPoolSize" value="3" />
<property name="minPoolSize" value="6" />
<property name="maxPoolSize" value="25" />
<property name="acquireIncrement" value="3" />
<property name="maxStatements" value="0" />
<!-- retries -->
<property name="acquireRetryAttempts" value="30" />
<property name="acquireRetryDelay" value="1000" /> <!-- 1s -->
<property name="breakAfterAcquireFailure" value="false" />
<!-- refreshing connections -->
<property name="maxIdleTime" value="180" /> <!-- 3min -->
<property name="maxConnectionAge" value="10" /> <!-- 1h -->
<!-- timeouts and testing -->
<property name="checkoutTimeout" value="5000" /> <!-- 5s -->
<property name="idleConnectionTestPeriod" value="60" /> <!-- 60 -->
<property name="testConnectionOnCheckout" value="true" />
<property name="preferredTestQuery" value="SELECT 1" />
<property name="testConnectionOnCheckin" value="true" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.app.db.entities</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory
</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.isolation">2</prop>
***<prop key="hibernate.implicit_naming_strategy">org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl</prop>***
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
On startup its still giving me the below error
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/hibernate-config.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.app.db.entities.Person column: street (should be mapped with insert="false" update="false")
As you can see I am using <prop key="hibernate.implicit_naming_strategy">org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl</prop> in the configuration . I thought this way you can have the embeddable object more than once in a class.
If i give a random string for the naming strategy its failing saying the class is not found. So I assume the configuration is read. Is there something I am missing here ? Any help is appreciated.
I've catched java.sql.SQLTransientConnectionException: springHikariCP - Connection is not available, request timed out after 30001ms.
First code block works well, second (CP) does not work.
What is wrong and how fix this?
JDK - 1.8.0_73.
HikariCP - 2.4.5.
Spring - 4.2.5.RELEASE.
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${connection.driverClassName}"/>
<property name="username" value="${connection.userName}"/>
<property name="password" value="${connection.password}"/>
<property name="url" value="${connection.url}"/>
</bean>
CP
<bean id="hikariConfiguration" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP"/>
<property name="dataSourceClassName" value="${connection.dataSourceClassName}"/>
<property name="maximumPoolSize" value="${connection.pool.maximumPoolSize}"/>
<property name="idleTimeout" value="${connection.pool.idleTimeout}"/>
<property name="dataSourceProperties">
<props>
<prop key="url">${connection.url}</prop>
<prop key="user">${connection.userName}</prop>
<prop key="password">${connection.password}</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy- method="close">
<constructor-arg ref="hikariConfiguration"/>
</bean>
//...............
#CONNECTION
connection.dataSourceClassName=org.hsqldb.jdbc.JDBCDataSource
connection.url=jdbc:hsqldb:mem:dbtest-local
connection.userName=sa
connection.password=
#POOL
connection.pool.maximumPoolSize=1
connection.pool.idleTimeout=28500
#HIBERNATE
hibernate.hbm2ddl.auto=create-drop
hibernate.dialect=H2Dialect
hibernate.show_sql=true
If you use the dataSourceClassName, you should not provide the jdbc url.
Instead, you should add the host, dbname, etc. as properties. See the example on github:
dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
dataSource.user=test
dataSource.password=test
dataSource.databaseName=mydb
dataSource.portNumber=5432
dataSource.serverName=localhost
Otherwise, you should not use the dataSourceClassName. Try with:
<bean id="hikariConfiguration" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="maximumPoolSize" value="${connection.pool.maximumPoolSize}" />
<property name="idleTimeout" value="${connection.pool.idleTimeout}" />
<property name="jdbcUrl" value="${connection.url}" />
<property name="dataSourceProperties">
<props>
<prop key="user">${connection.userName}</prop>
<prop key="password">${connection.password}</prop>
</props>
</property>
</bean>
This is working for me in local.
Hi this is what I am looking to do -
I have three databases - each exactly the same - one is primary and two and three are back ups.
I am using hibernate and spring to connect to the data base.I have spring application context file configured with 3 session factories, and 3 corresponding data sources.
I was wondering if there is a way using spring where if the application is not able to connect to the database 1 (an exception is thrown) it will connect to database 2 - and in turn is database 3 is down it will connect to 3 and proceed.
What would be the best way to implement this? I have read about AbstractRoutingDataSource ..but not sure how to make use of that in this case..also if there are other ideas.. Would appreciate some directions. Thanks!
this is my config in application context xml -
<bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="$g{jdbc.driverClassName}" />
<property name="jdbcUrl" value="$g{url1}" />
<property name="user" value="$l{uid1}" />
<property name="password" value="$l{pwd1}" />
<property name="minPoolSize" value="$g{jdbc.minPoolSize}" />
<property name="maxPoolSize" value="$g{jdbc.maxPoolSize}" />
<property name="preferredTestQuery" value="$g{jdbc.preferredTestQuery}" />
<property name="testConnectionOnCheckout" value="$g{jdbc.testConnectionOnCheckout}" />
</bean>
<bean id="sessionFactory1"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource1" />
<property name="packagesToScan">
<list>
<value>com.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">$g{jdbc.dialect}</prop>
<prop key="hibernate.show_sql">$g{jdbc.show_sql}</prop>
</props>
</property>
</bean>
<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="$g{jdbc.driverClassName}" />
<property name="jdbcUrl" value="$g{url2}" />
<property name="user" value="$l{uid2}" />
<property name="password" value="$l{pwd2}" />
<property name="minPoolSize" value="$g{jdbc.minPoolSize}" />
<property name="maxPoolSize" value="$g{jdbc.maxPoolSize}" />
<property name="preferredTestQuery" value="$g{jdbc.preferredTestQuery}" />
<property name="testConnectionOnCheckout" value="$g{jdbc.testConnectionOnCheckout}" />
</bean>
<bean id="sessionFactory2"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource2" />
<property name="packagesToScan">
<list>
<value>com.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">$g{jdbc.dialect}</prop>
<prop key="hibernate.show_sql">$g{jdbc.show_sql}</prop>
</props>
</property>
</bean>
<bean id="dataSource3" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="$g{jdbc.driverClassName}" />
<property name="jdbcUrl" value="$g{url3}" />
<property name="user" value="$l{uid3}" />
<property name="password" value="$l{pwd3}" />
<property name="minPoolSize" value="$g{jdbc.minPoolSize}" />
<property name="maxPoolSize" value="$g{jdbc.maxPoolSize}" />
<property name="preferredTestQuery" value="$g{jdbc.preferredTestQuery}" />
<property name="testConnectionOnCheckout" value="$g{jdbc.testConnectionOnCheckout}" />
</bean>
<bean id="sessionFactory3"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource3" />
<property name="packagesToScan">
<list>
<value>com.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">$g{jdbc.dialect}</prop>
<prop key="hibernate.show_sql">$g{jdbc.show_sql}</prop>
</props>
</property>
</bean>
<bean id="transactionManager1"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory1" />
</bean>
<bean id="transactionManager2"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory2" />
</bean>
<bean id="transactionManager3"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory3" />
</bean>
There is a way to achieve this by using Spring Abstract Data Source Routing mechanism -
https://spring.io/blog/2007/01/23/dynamic-datasource-routing/
I have successfully configured hibernate and I can run transactions but only from the psvm of the DAO class. I want to configure it with my spring app using the same configuration file i.e. hibernate.cfg.xml.
How can I do this? Most tutorials I've read simply neglect the hibernate configuration file.
You can add this code to you xml file to configure hibernate.
<!-- Hibernate Related Configuration. -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://192.168.1.9:5432/dbname"/>
<property name="username" value="postgres"/>
<property name="password" value="pwd"/>
<property name="validationQuery" value="SELECT 1"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.domain"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
</bean>
<!-- Transaction Manager -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
The hibernate.cfg.xml file is specified for the LocalEntityManagerFactoryBean, along with your DataSource
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath*:META-INF/hibernate.cfg.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
Here you can find an example of a Spring XML configuration containing some Hibernate configuration