Server stops to respond after using spring transaction management - java

I configured spring transaction management, and it is working as expected. However, when the load increases the server stops responding.
I am working on a multi module project that I have configured in database.xml, which is inside the DAO module
Screenshot of directory structure of my project:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<context:component-scan base-package="za.co.virginactive.dao" />
<context:property-placeholder location="classpath:jdbc.properties"/>
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="testConnectionOnCheckout" value="true"/>
<property name="testConnectionOnCheckin" value="true"/>
<property name="preferredTestQuery" value="SELECT first 1 clubno from club"/>
</bean>
<bean id="imageDS" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${image.jdbc.driverClassName}" />
<property name="jdbcUrl" value="${image.jdbc.url}" />
<property name="user" value="${image.jdbc.username}" />
<property name="password" value="${image.jdbc.password}" />
<property name="testConnectionOnCheckout" value="true"/>
<property name="testConnectionOnCheckin" value="true"/>
<property name="preferredTestQuery" value="SELECT first 1 clientno from images"/>
</bean>
<bean id="mdsDS" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${mds.jdbc.driverClassName}" />
<property name="jdbcUrl" value="${mds.jdbc.url}" />
<property name="user" value="${mds.jdbc.username}" />
<property name="password" value="${mds.jdbc.password}" />
<property name="testConnectionOnCheckout" value="true"/>
<property name="testConnectionOnCheckin" value="true"/>
<property name="preferredTestQuery" value="SELECT top 1 id from mdm.club"/>
</bean>
<bean id="accessDS" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${access.jdbc.driverClassName}" />
<property name="jdbcUrl" value="${access.jdbc.url}" />
<property name="user" value="${access.jdbc.username}" />
<property name="password" value="${access.jdbc.password}" />
<property name="testConnectionOnCheckout" value="true"/>
<property name="testConnectionOnCheckin" value="true"/>
<property name="preferredTestQuery" value="SELECT first 1 clubno from club"/>
</bean>
<bean id="archiveDS" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${archive.jdbc.driverClassName}" />
<property name="jdbcUrl" value="${archive.jdbc.url}" />
<property name="user" value="${archive.jdbc.username}" />
<property name="password" value="${archive.jdbc.password}" />
<property name="testConnectionOnCheckout" value="true"/>
<property name="testConnectionOnCheckin" value="true"/>
<property name="preferredTestQuery" value="SELECT first 1 * from rdb$database"/>
</bean>
<!--<jdbc:initialize-database data-source="dataSource">-->
<!--<jdbc:script location="classpath:ddl/db-schema.sql"/>-->
<!--</jdbc:initialize-database>-->
</beans>

Related

How to setup multiple data sources with Spring and jdbcTemplate with XML

I'm setting up two different datasource for diff databse in spring-servlet.xml
I have tried multiple solutions provided on sites most of them are for spring-boot.
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe" />
<property name="username" value="***" />
<property name="password" value="***" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSourceDev"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe" />
<property name="username" value="***" />
<property name="password" value="***" />
</bean>
<bean id="jdbcTemplateDev" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourceDev" />
</bean>
In java class -
#Autowired
#Qualifier("jdbcTemplate")
private JdbcTemplate jdbcTemplate;
#Autowired
#Qualifier("jdbcTemplateDev")
private JdbcTemplate jdbcTemplatedev;*/

Check Datasource connection during initialization

I am using org.apache.commons.dbcp.BasicDataSource and com.mchange.v2.c3p0.ComboPooledDataSource APIs to manage datasource connections.
When I have changed the correct connection parameters I was expecting these API gave an exception during initialization but they did not. What am I missing?
<bean id="uslDataSource1" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="false">
<property name="driverClassName" value="${usl.db.driverClass}"/>
<property name="url" value="${usl.db.jdbcUrl}"/>
<property name="username" value="${usl.db.username}"/>
<property name="password" value="${usl.db.password}"/>
<property name="initialSize" value="${usl.db.initialPoolSize}"/>
<property name="validationQuery" value="select 1 from dualx"/>
<property name="testOnBorrow" value="false"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="1200000"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="5"/>
<property name="defaultAutoCommit" value="true"/>
</bean>
<!-- the DataSource (parameterized for configuration via a PropertyPlaceHolderConfigurer) -->
<bean id="scpcDataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init="false">
<property name="driverClass" value="${scpc.db.driverClass}" />
<property name="jdbcUrl" value="${scpc.db.jdbcUrl}" />
<property name="user" value="${scpc.db.username}" />
<property name="password" value="${scpc.db.password}" />
<property name="initialPoolSize" value="${scpc.db.initialPoolSize}" />
<property name="minPoolSize" value="${scpc.db.minPoolSize}" />
<property name="maxPoolSize" value="${scpc.db.maxPoolSize}" />
<property name="acquireIncrement" value="${scpc.db.acquireIncrement}" />
<property name="autoCommitOnClose" value="${scpc.db.autoCommitOnClose}" />
<!-- property name="maxIdleTime" value="${scpc.db.maxIdleTime}" / -->
<property name="idleConnectionTestPeriod" value="${scpc.db.idleConnectionTestPeriod}" />
<property name="preferredTestQuery" value="${scpc.db.validationQuery}" />
<property name="maxStatements" value="${scpc.db.maxStatements}" />
</bean>

The configuration for com.mchange.v2.c3p0.ComboPooledDataSource

I am looking for help with configuration of my data source type com.mchange.v2.c3p0.ComboPooledDataSource
I need to have 1000 database connections available concurrently.
I started with following configuration and tried to increase the number for initialPoolSize, maxPoolSize, minPoolSize but it doesn’t work.
<bean id="dataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="someUrl"/>
<property name="user" value="someUser"/>
<property name="password" value="somePass"/>
<property name="acquireIncrement" value="1" />
<property name="acquireRetryAttempts" value="3"/>
<property name="acquireRetryDelay" value="300" />
<property name="initialPoolSize" value=“3" />
<property name="maxPoolSize" value="10" />
<property name="minPoolSize" value=“3" />
</bean>

Persistance.xml vs DispatcherServlet

I am using JPA 2.0, Hibernate 3 Annotation Version, the problem is that i am confusing in Persistence.xml & DispatcherServlet
My persistence.xml file contains:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="Hello" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>models.student</class>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/test"/>
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="admin" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
</properties>
</persistence-unit>
</persistence>
In my dispatcher servlet file I have following Beans:
<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/test" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="Hello" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
Question is: why do we need to put all properties (DriverClassName, URL, UserName, Password) in both persistence.xml and in DataSource bean in DispatcherServlet?
I am learning and it is confusing me, please help.
Actually you have double configuration here.
You need either persistence.xml or bean definitions.
Take a look here

DBCL message on console

I am creating a database connection pool with following properties.
<bean id="complianceCCRDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" lazy-init="false" >
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="username" value="${deshaw.compliance.ccr.db.username}" />
<property name="password" value="${deshaw.compliance.ccr.db.password}" />
<property name="url" value="${deshaw.compliance.ccr.db.url}" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
<property name="maxWait" value="60000"/>
<property name="testOnBorrow" value="true" />
<property name="validationQuery">
<value>use ${deshaw.compliance.ccr.db.name} SELECT 1
</value>
</property>
<property name="maxIdle" value="10" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="600" />
<property name="logAbandoned" value="false" />
</bean>
When I try to instantiate the above bean, I am getting following messages on STDOUT. Is there any way I can suppress these ?
AbandonedObjectPool is used (org.apache.commons.dbcp.AbandonedObjectPool#2d82ef6b)
LogAbandoned: false
RemoveAbandoned: true
RemoveAbandonedTimeout: 600
You should use the setLogWriter() (see javadoc here) method as described in this JIRA.

Categories