Spring jdbc transaction not commit - spring

I have following Spring configuration:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
Beans with database and transaction management are configured as below:
<bean id="sufe.ncm.docDao" class="DocDaoImpl" scope="singleton">
<property name="jdbcTemplate" ref="sufe.ncm.jdbcTemplate"/>
</bean>
<bean id="sufe.ncm.jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="sufe.ncm.filterDB"/>
<property name="fetchSize" value="5000"/>
</bean>
<bean id="sufe.ncm.filterDB" class="org.apache.commons.dbcp.BasicDataSource">
<property name="defaultAutoCommit" value="false" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="..."/>
<property name="username" value="..."/>
<property name="password" value="..."/>
<property name="initialSize" value="1"/>
<property name="minIdle" value="1"/>
<property name="maxIdle" value="5"/>
<property name="maxOpenPreparedStatements" value="5"/>
</bean>
<context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="sufe.ncm.filterDB"></property>
</bean>
DocDaoImpl class implements method updateData from interface DocDao:
#Transactional(rollbackFor = SQLException.class, propagation = Propagation.REQUIRES_NEW)
public synchronized void updateData() {
jdbcTemplate.batchUpdate(UPDATE_QUERY_1);
jdbcTemplate.batchUpdate(UPDATE_QUERY_1);
}
Because dataSource has set autocommit option on false, I expect that transaction will be committed only when both batchUpdates operation will finish whithout error. Unfortunatelly, unless both updates finishes whithout error, commit is not performed and I don't see any changes in database. Whats wrong ? Why #Transactional annotation doesn't work and commit is not performed ?

Related

Spring & Hibernate - Error when using #Transactional

I have an application setup based on a previous configuration - which was 100% functional. Now I am using maven and am encountering an Error in my controller class which says i must configure Beans.xml.
I'm not sure exactly what this means, as I have my dispatcher-servlet.xml configured in the same way as the functional application.
dispatcher-servlet.xml
<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Enables the Spring MVC #Controller programming model -->
<mvc:annotation-driven />
<context:component-scan base-package="com.me.test.controller"/>
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="url" value="jdbc:derby://localhost:1527/userDB"/>
<property name="username" value="username"/>
<property name="password" value="password"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userDao" class="com.me.test.dao.UserDAOImpl">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>
Edit: Screenshot of error. Which occurs when using the #Transactional annotation.

How to connect to multiple database using spring hibernate entitymanager

I have two databases for two different organizations with same database tables and objects. Now the application need to support both DB. Each user is attached to one organization and based on his login id application need to connect to particular DB and perform same operations.
So all Spring beans remain the same except target Database. How to do that most efficient way. I can think of creating multiple EntityManagerFactory in spring applicationContext file and in DAO I can select particular entitymanager based on user id/name (by passing it as an argument etc)
But what will be most efficient/correct way if we consider second level caching etc.
?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation=
"http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd">
<tx:annotation-driven />
<bean id="empSvc" class="com.techcielo.sampleproject.service.EmployeeService">
<property name="empDao" ref="empDao"></property>
</bean>
<bean id="empDao" class="com.techcielo.sampleproject.dao.EmployeeDAO">
<property name="fac" ref="entityManagerFactory"></property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="config/persistence.xml"></property>
<property name="dataSource" ref="dataSource_2" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
</bean>
<!-- Create one more entitymanager factory here with _2 and rename previous one with _1 -->
<bean id="dataSource_1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/northwind" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="dataSource_2" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/northwind_dup" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
</beans>
Thanks in advance.

test and applicationContext

i use latest release of spring, spring data, jpa, hibernate and h2.
i try to run test (only repository test).
but i get this error: Failed to load ApplicationContext
my test class
#RunWith(SpringJUnit4ClassRunner.class)
#TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
#Transactional
#ContextConfiguration(
locations = {"classpath:applicationContext-test.xml"})
public class UserDaoTest extends AbstractTransactionalJUnit4SpringContextTests {
...
}
my applicationContext-test.xml file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.2.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:property-placeholder location="classpath:/jdbc.properties"/>
<context:component-scan base-package="com.test.*" />
<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>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<jpa:repositories base-package="com.test.va.repository"/>
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="idleConnectionTestPeriodInMinutes" value="${jdbc.idleConnectionTestPeriodInMinutes}"/>
<property name="idleMaxAgeInSeconds" value="${jdbc.idleMaxAgeInSeconds}"/>
<property name="maxConnectionsPerPartition" value="${jdbc.maxConnectionsPerPartition}"/>
<property name="minConnectionsPerPartition" value="${jdbc.minConnectionsPerPartition}"/>
<property name="partitionCount" value="${jdbc.partitionCount}"/>
<property name="acquireIncrement" value="${jdbc.acquireIncrement}"/>
<property name="statementsCacheSize" value="${jdbc.statementsCacheSize}"/>
<property name="releaseHelperThreads" value="${jdbc.releaseHelperThreads}"/>
</bean>
so it's a config error.

property 'configurationClass' is not writable or has an invalid setter method. Does the parameter type of the setter

<?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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<util:properties id="hibernateProperties" location="classpath:hibernate.properties" />
<bean id="usermanagementSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="usermanagementDataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<property name="hibernateProperties" ref="hibernateProperties" />
</bean>
<jee:jndi-lookup id="usermanagementDataSource" jndi-name="java:jboss/datasources/usermanagementDS" />
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close">
<property name="forceShutdown" value="false" />
<property name ="startupTransactionService" value="true"/>
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="30" />
</bean>
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager" />
<property name="userTransaction" ref="atomikosUserTransaction" />
</bean>
<bean id="User" class="com.ecom.data.access.model.User"/>
<bean id="myFactory" class="com.ecom.data.access.dao.MyFactory"/>
</beans>
I am using hibernate 4 spring 3 maven 3, i have this configuratiobn file and here I am using local session factory and it compile correctly but it gives the error
when I am using the JBoss server to deploy it then server console gives the error 'configurationClass' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? please help me to sort out this problem
Your bean definition suggests that you are trying to configure Hibernate 3, not Hibernate 4. You have probably followed incorrect example or tutorial. In Hibernate 4 there is no configurationClass property. Just remove it:
<bean id="usermanagementSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="usermanagementDataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="hibernateProperties" ref="hibernateProperties" />
</bean>
With Hibernate 4, you also don't need to provide configuration XML. All you can do is to specify packages to be scanned for #Entity classes:
<property name="packagesToScan" value="com.ecom.data.access.model" />

Spring jpa transaction business to Dao

business layer:
public class ServiceImpl implements Service{
#Transactional(readOnly = false)
public void createOrUpdateAppPing(String s) {
servicePingDao.createOrUpdateAppPing(s);
}
}
Dao layer
public void createOrUpdateAppPing(String sc) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
pingScService(sc,entityManager);
if(ifHasPingScConfig(sc,entityManager)) {
updateScConfig(sc,entityManager);
} else {
createScConfig(sc,entityManager); }
entityManager.close();
}
here :
** pingScService has select query
** updateScConfig - update query
** createScConfig-insert
config service-datasorce.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" xmlns:osgi="http://www.springframework.org/schema/osgi"
xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"
xmlns:ctx="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd
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/osgi-compendium
http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd
">
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="smx4" />
<property name="jpaVendorAdapter" ref="jpaAdapter" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="com.luthresearch.savvyconnect.model.PostgreSQLDialectUuid" />
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${database.driver.class}" />
<property name="url" value="${database.savvyconnect.url}" />
<property name="username" value="${database.savvyconnect.username}" />
<property name="password" value="${database.savvyconnect.password}" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
IN service.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" xmlns:osgi="http://www.springframework.org/schema/osgi"
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/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="service"
class="com.services.impl.SServicempl" autowire="byName">
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
Now i want to add transaction support at service layer ...How i can do this.
I tried adding #Transaction annotation but I am getting
javax.persistence.TransactionRequiredException: Executing an update/delete query

Resources