I am using maven-java-spring with quartz schedular and spring-jdbc template.
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.7.3</version>
</dependency>
We have process which makes large amount of inserts using quartz scheduler job.
When i execute job after inserting x number of records it throws exception as follows
exception caught: org.springframework.transaction.TransactionSystemException: Could not commit JDBC transaction; nested exception is java.sql.SQLException: commit() should not be called while in auto-commit mode.
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doCommit(DataSourceTransactionManager.java:270)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:147)
Following is my sechdular code
<!-- Spring jobs -->
<bean id="wireJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="search"/>
<property name="targetMethod" value="executeWireSearch"/>
</bean>
<bean id="nonWireJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="search"/>
<property name="targetMethod" value="executeNonWireSearch"/>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="wireQuartzTrigger"/>
<ref bean="nonWireQuartzTrigger"/>
</list>
</property>
</bean>
does it something to do with quartz properties related to autocomit ??
Give a look to the quartz FAQ: http://quartz-scheduler.org/documentation/faq#FAQ-springCommit
BTW, you should upgrade if possible because 1.7 is quite old and not supported anymore
HIH
Related
Working on a Spring 5 and Hibernate Project. Using Spring for bean management, transaction and MVC. The changes are not getting committed to database though i can see the insert statement in the log. There is no error. There is no issue with select statements. I could login into the application. Below are my configurations:
framework.xml:
<context:component-scan base-package="com.test" >
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource"
ref="dataSource"/>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="jtaTransactionManager" ref="transactionManager" />
</bean>
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown" value="false" />
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.J2eeUserTransaction">
<property name="transactionTimeout" value="300" />
</bean>
<!--
<bean id="HibernateTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
-->
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" >
<property name="transactionManager"><ref bean="atomikosTransactionManager" /></property>
<property name="userTransaction"><ref bean="atomikosUserTransaction" /></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/>
In the above configuration, i excluded the controllers as they are loaded using a different mvc related config files.
The service classes were annotated with #Transactional.
I tried without JTA with plain HibernateTransactionManager also. The transactions are not getting committed.
The below entry is also for hibernate to use JTA as transaction manager
hibernate.transaction.jta.platform to org.hibernate.engine.transaction.jta.
platform.internal.AtomikosJtaPlatform and
hibernate.transaction.coordinator_class to jta
I am using getCurrentSession for getting hibernate session.
I have to use Atomikos as JTA transaction manager as the development has to be happen in servlet container.
Thanks in advance for any help to find the gap in the configuration or any other issues..
The problem was with the maven Jetty plugin and the db connection created didn't persist the changes with hibernate 5 with maven jetty plugin 9.4.35.v20201120 though it was working with hibernate 3 and maven jetty plugin 7.0.1.v20091125. When I deployed the same war file with pre-configured data source in tomcat, able to persist the changes.
I am working on TIBCO BusinessWorks 5.3. So we normally provide query timeout in the SQL Direct/JDBC Query activity. But for Ignite cache that timeout does no seem to work.
This is installed on a Linux server. I tried to add the setTimeout property in the config xml under cacheConfiguration property node.
I have tried with 2 different configurations
1.
<bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
<property name="cacheConfiguration">
<list>
<bean class="org.apache.ignite.configuration.CacheConfiguration">
<!--some properties-->
<property name="setTimeout" value="60" />
</bean>
</list>
</property>
2.
<bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
<property name="cacheConfiguration">
<list>
<bean class="org.apache.ignite.configuration.CacheConfiguration">
<!--some properties-->
</bean>
<bean class="org.apache.ignite.cache.query.SqlFieldsQuery">
<property name="setTimeout" value="60" />
</bean>
</list>
</property>
Error message is thrown as below-
org.springframework.beans.NotWritablePropertyException: Invalid
property setTimeout of bean class
[org.apache.ignite.configuration.CacheConfiguration]: Bean property
setTimeout is not writable or has an invalid setter method.
Currently you may use SqlQuery/SqlFieldsQuery API to set individual timeouts for queries: https://apacheignite-sql.readme.io/docs/query-cancellation
It's a known issue that there is no option to configure default timeout for a query, here is link for your reference (there is a PR that was active month ago): https://issues.apache.org/jira/browse/IGNITE-7285
JDBC query timeout is implemented, but not yet documented/released: https://issues.apache.org/jira/browse/IGNITE-5438
I'm stumbled upon a problem while developing a Web Application based on Struts2 with Spring and Hibernate.
When I refresh the site a few times, for example 2-4 times, Hibernate is showing up an exception about Too many connections. I've tried to implement C3P0 Connection pool and have some problems with it
The hibernate.cfg.xml configuration:
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/axelle</property>
<property name="hibernate.connection.username">axelle</property>
<property name="hibernate.connection.password">dbpassword</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
applicationContext.xml
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:jdbc.properties"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
p:connectionProperties="${jdbc.connectionProperties}"/>
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
The log output is:
org.hibernate.exception.JDBCConnectionException: Cannot open connection
and:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
And this is how PROCESSLIST MySQL window looks: http://img844.imageshack.us/img844/3959/be69273cc2.png
I've set max_size of connections to 20, but it seems like it doesn't read the C3P0 configuration from file, cause from the screen we can see that number of connections is higher than 20, or maybe I'm doing something wrong, but where? I really need some help guys, I'll appreciate this, and thanks in advance.
Mention these property in your hibernate.cfg.xml file
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.timeout">100</property>
Refer this link for better understanding: Configuration Properties
In your applicationContext, instead of using <bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" ... try using <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" ...
The C3P0PooledDataSource will create a wrapped database connection using the specified DriverClassName, url, username and password
I do have a working configuration in Spring for JPA with Hibernate provider:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<tx:annotation-driven mode="aspectj"/>
<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>
This configuration is working in a small Spring based web-app.
But, when I insert the same configuration into an other existing Spring based web-app, I do get the following exception:
javax.persistence.TransactionRequiredException: no transaction is in progress
I think it has some conflict with Spring JDBC Templates:
<bean id="mysqlTemplate"
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
How can I get this to work side by side?
"no transaction in progress" just means you're trying to use an EntityManager somewhere and you didn't start a transaction first. The stack trace will tell you exactly where that is. Since you're using annotation-driven transactions, just make sure you have an appropriate #Transactional somewhere up the call chain from the location where the exception is happening.
I tried to setup infinispan as 2nd level cache for hibernate in spring+tomcat based app.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${hibernate.connection.driver_class}"/>
<property name="url" value="${hibernate.connection.url}"/>
<property name="username" value="${hibernate.connection.username}"/>
<property name="password" value="${hibernate.connection.password}"/>
<property name="maxActive" value="${hibernate.connection.maxActive}"/>
<property name="maxIdle" value="${hibernate.connection.maxIdle}"/>
<property name="minIdle" value="${hibernate.connection.minIdle}"/>
<property name="maxWait" value="${hibernate.connection.maxWait}"/>
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager">
<bean class="com.arjuna.ats.jta.TransactionManager" factory-method="transactionManager"/>
</property>
<property name="userTransaction">
<bean class="com.arjuna.ats.jta.UserTransaction" factory-method="userTransaction"/>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.example"/>
<!---->
<property name="hibernateProperties" ref="db-properties"/>
</bean>
And the properties are:
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.infinispan.InfinispanRegionFactory
I get an exception:
Caused by: org.infinispan.CacheException: This is transactional cache but no transaction manager could be found. Configure the transaction manager lookup properly.
How do I configure a transaction manager lookup?
The easy answer for spring is to add (inside id="sessionFactory" spring XML configuration) :
<property name="jtaTransactionManager" ref="transactionManager"/>
This is available in spring 3.2.2.RELEASE (it maybe available in older spring versions too, I would guess since spring 3.1.x)
This should achieve what Steve Ebersole points at. This causes Spring to provide a JtaPlatform via the class https://github.com/SpringSource/spring-framework/blob/master/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/ConfigurableJtaPlatform.java
See JavaDoc for class org.springframework.orm.hibernate4.LocalSessionFactoryBean (that you are using) concerning JTA usage.
Both Hibernate and Infinispan need to know about the TransactionManager. If you tell Hibernate about the TransactionManager, it will in turn tell Infinispan for you. I am guessing Spring has a way to tell Hibernate about the JTA setup, but I could not find it. Hibernate for its part (I see you are at least trying to use Hibernate 4) needs to be told about which org.hibernate.service.jta.platform.spi.JtaPlatform to use. org.hibernate.service.jta.platform.spi.JtaPlatform is the contract for Hibernate to know how to interact with the JTA environment.