Announcement Announcement Module
Collapse
No announcement yet.
JpaRepository and transaction management Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JpaRepository and transaction management

    I have written some code that will load jobs (~3000), processes them involving various web service calls, and then updates the job status, and writes a log to a log table. I am using JDK 1.6, Jboss AS 7.1 and Spring 3.

    When my jobs are processing, I do not see any activity in the database until all of the jobs have complete. This takes about 1 hour or so.

    I was thinking that my use of @Transactional around the service method that interacts with the JPA repositories would result in a new transaction being created and committed each time the method is called. However, that doesn't seem to be the case. Instead, it appears that all of the transactions are being committed in a single entry at the end. The reason I say that is due to the fact that I cannot see any database updates until the scheduler call to processJobs completes.

    Any help or direction would be greatly appreciated.

    Thanks in advance.....J

    JPA Entity
    Code:
    @Entity
    @Table(name = "log")
    public class Log implements Serializable {
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        @Column(name = "id", nullable = false, insertable = true, updatable = true, length = 11, precision = 0)
        private Integer id;
    
        .....
    }
    JPA Entity
    Code:
    @Entity
    @Table(name = "job")
    public class Job implements Serializable {
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        @Column(name = "id", nullable = false, insertable = true, updatable = true, length = 11, precision = 0)
        private Integer id;
    
        .....
    }
    Repository
    Code:
    public interface LogRepository extends JpaRepository<Log, Integer> {
    
    }
    Repository
    Code:
    public interface JobRepository extends JpaRepository<Job, Integer> {
    
    }
    Service
    Code:
    @Service
    public class LogServiceImpl implements LogService {
    
        @Autowired(required=true)
        private LogRepository logRepository;
    
        @Autowired(required=true)
        private JobRepository jobRepository;
    
        /**
         * Called by the scheduler
         */
        public void processJobs() {
    
            List<Job> jobs= jobRepository.findAll();
    
            for (Job job : jobs) {
                processJob(job);
            }
        }
    
        @Transactional
        private void processJob(Job job ) {
            Log log = new Log();
            log.setJob(job);
            log.setStartTime(new Date());
            try {
                
                // Process job, calling jax-ws services.
                job.setComplete();
                jobRepository.saveAndFlush(job);
            } catch (Exception e) {
                log.setResult("Error");
            } finally {
                log.setFinishTime(new Date());
                logRepository.saveAndFlush(log);
            }
        }
    }

    Code:
    @Service
    public class JobQueueScheduler {
    
        public JobQueueScheduler () {       
        }
    
        @Autowired(required=true)
        private LogService logService ;
    
    
        @Scheduled(cron = "0 0/5 * ? * *")
        public void processJobs() {
            logService.processJobs();
        }
    }
    applicationContext.xml
    Code:
    <?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"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <import resource="classpath:META-INF/scheduler-context.xml" />
    </beans>
    scheduler-context.xml
    Code:
    <?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:task="http://www.springframework.org/schema/task"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <import resource="classpath:META-INF/jpa-context.xml" />
    
        <context:property-placeholder location="classpath:*.properties"/>
    
        <context:component-scan base-package="com.example"/>
    
        <task:annotation-driven/>
    
    </beans>
    jpa-context.xml
    Code:
    <?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"
           xmlns:jee="http://www.springframework.org/schema/jee"
           xmlns:jpa="http://www.springframework.org/schema/data/jpa"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">
    
        <context:property-placeholder location="classpath:*.properties"/>
    
    	<context:component-scan base-package="com.example" />
    
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="exampleXADatasource"/>
            <property name="persistenceUnitName" value="examplePU"/>
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="showSql" value="true"/>
                    <property name="generateDdl" value="false"/>
                    <property name="database" value="MYSQL"/>
                    <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
                </bean>
            </property>
            <property name="jpaDialect">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
            </property>
        </bean>
    
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
        </bean>
    
        <jee:jndi-lookup id="exampleXADatasource" jndi-name="java:/ExampleXADataSource"/>
    
        <tx:annotation-driven transaction-manager="transactionManager"/>
    
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    
        <jpa:repositories base-package="com.example.repo"/>
    
    </beans>
Working...
X