File to file basics - spring-batch

Being new to Spring Batch, I wanted to begin with something simple....Reading a csv file and writing the same objects(records) to another one. Simple, isn't it ? But I was unable to find a working sample. After some time of research, I found some things which almost work....The file I want to write to is allways empty. Is it because I use a ressourcelesstransactionmanager ? Do I need to declare some optional property somewhere to flush the thing on my hard drive ?
By the way, I find the documentation on the subject very light and confusing, for a beginner. Maybe it's because one must earn spring Batch...
Here's the evil but very simple code that's driving me crazy.
TIA.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
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-2.5.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-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/tx
http://www.springframework.org/schema/tx/spring-tx-2.1.xsd">
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.SimpleJobRepository">
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.dao.MapJobInstanceDao"/>
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.dao.MapJobExecutionDao"/>
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.dao.MapStepExecutionDao"/>
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.dao.MapExecutionContextDao"/>
</constructor-arg>
</bean>
<bean id="jobRepository-transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher"
p:jobRepository-ref="jobRepository"/>
<!-- Les bean projet -->
<bean id="csvReader"
class="org.springframework.batch.item.file.FlatFileItemReader"
p:resource="file:c:\AppPerso\csvManager\client.csv">
<property name="lineMapper">
<bean
class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"
p:delimiter=";"
p:names="client_id;client_status;client_name;client_surname;client_dnais;client_addr1;client_addr2;client_addr3;client_addr4"/>
</property>
<property name="fieldSetMapper">
<bean
class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper"
p:targetType="com.bigmac.spring.batch.csv.Client"/>
</property>
</bean>
</property>
</bean>
<bean id="csvWriter"
class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="resource" value="file:c:\AppPerso\csvManager\client-reformat.csv"/>
<property name="shouldDeleteIfExists" value="true"/>
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value=";"/>
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="client_id;client_status;client_name;client_surname;client_dnais;client_addr1;client_addr2;client_addr3;client_addr4"/>
</bean>
</property>
</bean>
</property>
</bean>
<batch:job id="CSVManager" job-repository="jobRepository">
<batch:step id="step1">
<batch:tasklet
transaction-manager="jobRepository-transactionManager">
<batch:chunk
reader="csvReader"
writer="csvWriter"
commit-interval="10"/>
</batch:tasklet>
</batch:step>
</batch:job>
</beans>

Sorry guys.
My mistake. I was getting confused with delimiters...The lack of properly configured log4j wasn't helping either.
I put the working configuration here.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
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-2.5.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-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/tx
http://www.springframework.org/schema/tx/spring-tx-2.1.xsd">
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.SimpleJobRepository">
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.dao.MapJobInstanceDao"/>
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.dao.MapJobExecutionDao"/>
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.dao.MapStepExecutionDao"/>
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.dao.MapExecutionContextDao"/>
</constructor-arg>
</bean>
<bean id="jobRepository-transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher"
p:jobRepository-ref="jobRepository"/>
<!-- Les bean projet -->
<bean id="csvReader"
class="org.springframework.batch.item.file.FlatFileItemReader"
p:resource="file:c:\AppPerso\springBatch\csvManager\client.csv">
<property name="lineMapper">
<bean
class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"
p:delimiter=";"
p:names="client_id,client_status,client_name,client_surname,client_dnais,client_addr1,client_addr2,client_addr3,client_addr4"/>
</property>
<property name="fieldSetMapper">
<bean
class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper"
p:targetType="com.bigmac.spring.batch.csv.Client"/>
</property>
</bean>
</property>
</bean>
<bean id="csvWriter"
class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="resource" value="file:c:\AppPerso\springBatch\csvManager\client-reformat.csv"/>
<property name="shouldDeleteIfExists" value="true"/>
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value=";"/>
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="client_id,client_status,client_name,client_surname,client_dnais,client_addr1,client_addr2,client_addr3,client_addr4"/>
</bean>
</property>
</bean>
</property>
</bean>
<batch:job id="CSVManager" job-repository="jobRepository">
<batch:step id="step1">
<batch:tasklet
transaction-manager="jobRepository-transactionManager">
<batch:chunk
reader="csvReader"
writer="csvWriter"
commit-interval="10"/>
</batch:tasklet>
</batch:step>
</batch:job>
</beans>

Related

How to integrate Apache Ignite with Cassandra as third party persistence?

I tried out below configurations but I get ClassNotFoundException for org.apache.ignite.cache.store.cassandra.CassandraCacheStoreFactory. I am using docker image of apache ignite to achieve the same and its version is 2.9.1.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
<property name="cacheConfiguration">
<list>
<!-- Configuring persistence for "cache1" cache -->
<bean class="org.apache.ignite.configuration.CacheConfiguration">
<property name="name" value="cache1"/>
<!-- Tune on Read-Through and Write-Through mode -->
<property name="readThrough" value="true"/>
<property name="writeThrough" value="true"/>
<!-- Specifying CacheStoreFactory -->
<property name="cacheStoreFactory">
<bean class="org.apache.ignite.cache.store.cassandra.CassandraCacheStoreFactory">
<!-- Datasource configuration bean which is responsible for Cassandra connection details -->
<property name="dataSourceBean" value="cassandraDataSource"/>
<!-- Persistent settings bean which is responsible for the details of how objects will be persisted to Cassandra -->
<property name="persistenceSettingsBean" value="primitive_csndra_cache"/>
</bean>
</property>
</bean>
</list>
</property>
</bean>
<bean id="loadBalancingPolicy" class="com.datastax.driver.core.policies.TokenAwarePolicy">
<constructor-arg type="com.datastax.driver.core.policies.LoadBalancingPolicy">
<bean class="com.datastax.driver.core.policies.RoundRobinPolicy"/>
</constructor-arg>
</bean>
<bean id="cassandraAdminDataSource"
class="org.apache.ignite.cache.store.cassandra.datasource.DataSource">
<property name="port" value="9042" />
<property name="contactPoints" value="mycassandra.default.svc.cluster.local" />
<property name="readConsistency" value="ONE" />
<property name="writeConsistency" value="ONE" />
<property name="loadBalancingPolicy" ref="loadBalancingPolicy" />
</bean>
<bean id="primitive_csndra_cache" class="org.apache.ignite.cache.store.cassandra.persistence.KeyValuePersistenceSettings">
<constructor-arg type="java.lang.String">
<value><![CDATA[
<persistence keyspace="hello" table="primitive_xyz">
<keyPersistence class="java.lang.String" strategy="PRIMITIVE" column="key"/>
<valuePersistence class="java.lang.String" strategy="PRIMITIVE" column="value"/>
</persistence>]]>
</value>
</constructor-arg>
</bean>
</beans>
Can anyone help me out on this? Any sort of sample github project or blog reference will also work out for me.
Take a look at these docs: https://ignite.apache.org/docs/latest/extensions-and-integrations/cassandra/configuration
and examples: https://ignite.apache.org/docs/latest/extensions-and-integrations/cassandra/usage-examples
overview: https://ignite.apache.org/docs/latest/extensions-and-integrations/cassandra/overview

Spring Batch MultiResourceItemWriter doesn't correctly writes data in files

This is my SpringBatch maven depedency:
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
Below is my job.xml file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<import resource="../config/launch-context.xml" />
<bean id="inputFileForMultiResource" class="org.springframework.core.io.FileSystemResource" scope="step">
<constructor-arg value="src/main/resources/files/customerInputValidation.txt"/>
</bean>
<bean id="outputFileForMultiResource" class="org.springframework.core.io.FileSystemResource" scope="step">
<constructor-arg value="src/main/resources/files/xml/customerOutput.xml"/>
</bean>
<bean id="readerForMultiResource" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="resource" ref="inputFileForMultiResource" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names" value="firstName,middleInitial,lastName,address,city,state,zip" />
<property name="delimiter" value="," />
</bean>
</property>
<property name="fieldSetMapper">
<bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="customer5TO" />
</bean>
</property>
</bean>
</property>
</bean>
<bean id="customer5TO" class="net.gsd.group.spring.batch.tutorial.to.Customer5TO"></bean>
<bean id="xmlOutputWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
<property name="resource" ref="outputFileForMultiResource" />
<property name="marshaller" ref="customerMarshallerJMSJob" />
<property name="rootTagName" value="customers" />
</bean>
<bean id="customerMarshallerJMSJob" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="aliases">
<map>
<entry key="customer" value="net.gsd.group.spring.batch.tutorial.to.Customer5TO"></entry>
</map>
</property>
</bean>
<bean id="multiResourceItemWriter" class="org.springframework.batch.item.file.MultiResourceItemWriter">
<property name="resource" ref="customerOutputXmlFile"/>
<property name="delegate" ref="xmlOutputWriter"/>
<property name="itemCountLimitPerResource" value="3"/>
<property name="resourceSuffixCreator" ref="suffix"/>
</bean>
<bean id="suffix" class="net.gsd.group.spring.batch.tutorial.util.CustomerOutputFileSuffixCreator"></bean>
<batch:step id="multiResourceWriterParentStep">
<batch:tasklet>
<batch:chunk
reader="readerForMultiResource"
writer="multiResourceItemWriter"
commit-interval="3">
</batch:chunk>
</batch:tasklet>
</batch:step>
<batch:job id="multiResourceWriterJob">
<batch:step id="multiResourceStep" parent="multiResourceWriterParentStep"/>
</batch:job>
</beans>
Basically I have one input file and more output files. I read the data from the input file (which contains 10 rows) and I want to write it by chunks of 3 into more than one output file (in my case there will be 4 files 3/3/3/1). For each chunk there will be one file.
The job works correctly but the content of the output files is wrong.
Each of the files contain the last element that has bean read in the current chunk.
Let's say the first chunk reads A, B and C. When this chunk is written to the file only the C is written and is written 3 times.
From my tests it works correctly only when the chunk commit-interval is 1.
Is this the correct behaviour?
What am I doing wrong?
Thank you in advance
Your customer5TO bean should have scope prototype. Currently it is a singleton, so the bean properties will always be overwritten with the last item.

xml schema for Springbatch3.0.1?

i am trying to accomplish spring batch in spring 3.0.1 and sufficient jars have been included in the class path.when i am trying to run the project with command line job runner i am getting a error that shows because of miss match xml schema for spring3.0.1. i have 3 differnt xml files as below
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<import resource="JobRepository.xml" />
</beans>
jobConfiguration.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<import resource="ApplicationContext.xml" />
<bean abstract="true"
class="org.springframework.batch.core.step.tasklet.TaskletStep" id="taskletStep">
<property name="jobRepository" ref="jobRepository" />
<property name="transactionManager" ref="transactionManager" />
</bean>
<bean class="com.mypackage.HelloWorldTasklet" id="helloTasklet">
<property name="message" value="Hello World!" />
</bean>
<bean class="com.mypackage.HelloWorldTasklet" id="createdByTasklet">
<property name="message" value="Created By Harish On Java" />
</bean>
<bean class="org.springframework.batch.core.job.SimpleJob" id="mySimpleJob">
<property name="name" value="mySimpleJob" />
<property name="steps">
<list>
<bean parent="taskletStep">
<property name="tasklet" ref="helloTasklet" />
</bean>
<bean parent="taskletStep">
<property name="tasklet" ref="createdByTasklet" />
</bean>
</list>
</property>
<property name="jobRepository" ref="jobRepository" />
</bean>
</beans>
JobRepository.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean
class="org.springframework.batch.core.repository.support.SimpleJobRepository"
id="jobRepository">
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.dao.MapJobInstanceDao" />
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.dao.MapJobExecutionDao" />
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.dao.MapStepExecutionDao" />
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.dao.MapExecutionContextDao" />
</constructor-arg>
</bean>
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean class="org.springframework.batch.core.launch.support.SimpleJobLauncher"
id="jobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
</beans>
Java Code:
public class HelloWorldTasklet implements Tasklet {
private String message;
public void setMessage(String message) {
this.message = message;
}
#Override
public RepeatStatus execute(StepContribution contribution,
ChunkContext context) throws Exception {
// TODO Auto-generated method stub
System.out.println(message);
return RepeatStatus.FINISHED;
}
}
my lib contained following jars
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.lang-sources-2.1.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.log4j-1.2.15.jar
commons-beanutils-1.8.3.jar
commons-codec-1.3.jar
commons-collections-3.2.jar
commons-digester-1.8.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
org.springframework.aop-3.0.1.RELEASE-A.jar
org.springframework.asm-3.0.1.RELEASE-A.jar
org.springframework.aspects-3.0.1.RELEASE-A.jar
org.springframework.beans-3.0.1.RELEASE-A.jar
org.springframework.context-3.0.1.RELEASE-A.jar
org.springframework.context.support-3.0.1.RELEASE-A.jar
org.springframework.core-3.0.1.RELEASE-A.jar
org.springframework.expression-3.0.1.RELEASE-A.jar
org.springframework.web-3.0.1.RELEASE-A.jar
org.springframework.web.servlet-3.0.1.RELEASE-A.jar
spring-batch-core-2.2.0.RELEASE.jar
spring-batch-infrastructure-2.2.0.RELEASE.jar
any response is appreciated greatly in advance.
You have the Spring Batch 2.2.0.RELEASE jars on your classpath, yet you aren't specifying that version in your schemas. Update to use the versions and you'll be fine.
As a side note, is there a reason you are not using the schemas in your configuration? While you're configuring batch components, you're doing it the hard way by not using the namespace or factory beans. For example, instead of:
<bean
class="org.springframework.batch.core.repository.support.SimpleJobRepository"
id="jobRepository">
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.dao.MapJobInstanceDao" />
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.dao.MapJobExecutionDao" />
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.dao.MapStepExecutionDao" />
</constructor-arg>
<constructor-arg>
<bean
class="org.springframework.batch.core.repository.dao.MapExecutionContextDao" />
</constructor-arg>
</bean>
you can do this:
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="transactionManager" ref="transactionManager"/>
</bean>
We also have namespace support for the job configuration. Instead of:
<bean class="org.springframework.batch.core.job.SimpleJob" id="mySimpleJob">
<property name="name" value="mySimpleJob" />
<property name="steps">
<list>
<bean parent="taskletStep">
<property name="tasklet" ref="helloTasklet" />
</bean>
<bean parent="taskletStep">
<property name="tasklet" ref="createdByTasklet" />
</bean>
</list>
</property>
<property name="jobRepository" ref="jobRepository" />
</bean>
you should try this:
<batch:job id="mySimpleJob">
<batch:step id="step1" next="step2">
<batch:tasklet ref="helloTasklet"/>
</batch:step>
<batch:step id="step2">
<batch:tasklet ref="createdByTasklet"/>
</batch:step>
</batch:job>
The namespaces handle some niceties like auto wiring the job repositories and transaction managers, etc. I'd recommend you take a look at the Spring Batch documentation here: http://docs.spring.io/spring-batch/2.2.x/reference/html/index.html

Spring Batch Caused by: java.lang.IllegalStateException: Existing transaction detected in JobRepository

I am getting this error in while running the batch job. I am very much new to Spring Batch so guys plz help as i didn't knw wht to do with this error. There is no other Batch job running in my system also .
My root-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
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: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/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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<import resource="job-context.xml"/>
<aop:aspectj-autoproxy />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg><ref bean="dataSource" /></constructor-arg>
</bean>
<bean id="dataSource" class="com.qr.jadu.tx.conpoolrouter.DataSourceRouter">
<property name="targetDataSources">
<map key-type="com.qr.jadu.tx.conpoolrouter.TransactionTypes">
<entry key="RO" value-ref="schedules-read-dataSource" />
<entry key="RW" value-ref="schedules-write-dataSource" />
</map>
</property>
<property name="defaultTargetDataSource" ref="schedules-read-dataSource" />
</bean>
<bean id="masters-read-dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/masterDatabase"/>
</bean>
<bean id="schedules-read-dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/scheduleDatabase"/>
</bean>
<bean id="schedules-write-dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/scheduleDatabase"/>
</bean>
<bean id="security-read-dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/sysDs"/>
</bean>
<bean id="csp-read-dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/cspDatabase"/>
</bean>
<bean id="schedules-punitpostprocessor" class="com.qr.jadu.entity.helper.JaduPersistenceUnitPostProcessor">
<property name="locationPatterns">
<list>
<value>classpath*:com/croamis/**/*.class</value>
</list>
</property>
</bean>
<alias name="schedules-dataSource" alias="lov-dataSource"/>
<bean id="schedules-entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<value>com.croamis.schedules</value>
</property>
<property name="persistenceUnitName" value="schedulesPU" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
</bean>
</property>
<property name="persistenceUnitPostProcessors">
<list>
<ref bean="schedules-punitpostprocessor" />
</list>
</property>
<property name="jpaProperties">
<!-- To run with MySQL -->
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
</props>
<!-- To run with Oracle -->
<!--
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
-->
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="schedules-entityManagerFactory" />
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />
My job-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:task="http://www.springframework.org/schema/task"
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.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema /batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<job id="GenerateOperationalFlightJob" xmlns="http://www.springframework.org/schema/batch">
<step id="step1">
<tasklet task-executor="taskexecutor">
<chunk reader="ScheduleRecordsAdapter" processor="scheduleRecordProcessor" writer="operationalFlightWriter" commit-interval="1">
</chunk>
</tasklet>
</step>
<bean id="ScheduleRecordsAdapter" class="org.springframework.batch.item.adapter.ItemReaderAdapter">
<property name="targetObject" ref="scheduleRecordsRetrievalAdapter"/>
<property name="targetMethod" value="getScheduleRecord"/>
</bean>
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseType" value="mysql" />
<property name="isolationLevelForCreate" value="ISOLATION_DEFAULT"/>
</bean>
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
</beans>

Using Spring data JPA with MS SQL server

I am setting up an app using Spring data JPA, Hibernate and MS SQL Server and unfortunately, I got a beat messed up with the configurations.
I hope someone here code make things clearer:.
This is my mvc-dispatcher.xml which is the application context:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<context:component-scan base-package="com.yyy.yyy" />
<context:property-placeholder location="classpath:db.properties"/>
<!-- DATA BASE -->
<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 name="generateDdl" value="true" />
<property name="database" value="SQL_SERVER" />
</bean>
</property>
<property name="persistenceUnitName" value="punit" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClass}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.pwd}" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<jpa:repositories base-package="com.yyy.yyy.yyy.repository"/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Now, I also have a persistence.xml:
<?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="punit">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
<property name="hibernate.connection.url" value="${jdbc.url}" />
<property name="hibernate.connection.driver_class" value="${jdbc.driverClass}" />
<property name="hibernate.connection.username" value="${jdbc.user}" />
<property name="hibernate.connection.password" value="${jdbc.pwd}" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>
I don't understand where should I put the DB connection data?
And I also cant connect to the DB (I get connection refused) - I am getting exception at server startup.
My project runs on Tomcat and I used https://github.com/SpringSource/spring-data-jpa-examples as a template.
EDIT
I managed to overcome the problems and publish to tomcat successfully,by removing the persistence XML file and use only the spring context file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx" 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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="yyy.yyy.yyy" />
<context:property-placeholder location="classpath:db.properties" />
<!-- DATA BASE -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="yyy.yyy.yyy.yyy.domain" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="showSql" value="true" />
<property name="database" value="SQL_SERVER" />
<property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" />
</bean>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="url" value="jdbc:jtds:sqlserver://localhost;databaseName=db" />
<property name="username" value="yyy" />
<property name="password" value="yyyyy" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<jpa:repositories base-package="yyy.yyy.yyy.yyy.repository" />
<!-- MVC -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
The app now loads fine, but when I use my repository and call save, I get connection refused exception.
Anyone knows why?
Help please.
Idob
You are refering to db.properites file here:<context:property-placeholder location="classpath:db.properties"/>
Where is it located in the project?