how to fix no transaction is in progress in spring batch












0















when I try persist some data that I recovered from my csv file on my database using my job processor in spring batch this error appear in the console , for my dao i'm using hibernate



I already tried 2 methode but the same probleme !



first :



    Session session = factory.getCurrentSession();
session.saveOrUpdate(p);


second :



    Session session = factory.openSession();
session.beginTransaction();
session.save(p);
session.getTransaction().commit();
session.close();


data source in my spring xml config :
all my spring xml config here https://pastebin.com/RZPr1GKL



<bean name="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/yassir" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>


<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>tp.entities.Personne</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>


<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<tx:annotation-driven />


the error :
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl.java:3450)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1418)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1414)
...










share|improve this question























  • 1. Since you're using Hibernate, you should use a HibernateTransactionManager. 2. Since you're using annotation-based transactions, the bean method that is supposed to be transactional should be annotated with @Transactional. docs.spring.io/spring/docs/current/spring-framework-reference/…

    – JB Nizet
    Dec 28 '18 at 16:33











  • I added the annotation @Transactional public void savePersonne(Personne p) { Session session = factory.getCurrentSession(); session.saveOrUpdate(p); } but the problem is still there

    – hamwac5
    Dec 28 '18 at 16:42











  • Well, apparently you read the second part of my comment, but not the first one.

    – JB Nizet
    Dec 28 '18 at 16:42











  • how to use a HibernateTransactionManager. ?

    – hamwac5
    Dec 28 '18 at 16:58











  • As shown in the link I posted in my first comment.

    – JB Nizet
    Dec 28 '18 at 16:58
















0















when I try persist some data that I recovered from my csv file on my database using my job processor in spring batch this error appear in the console , for my dao i'm using hibernate



I already tried 2 methode but the same probleme !



first :



    Session session = factory.getCurrentSession();
session.saveOrUpdate(p);


second :



    Session session = factory.openSession();
session.beginTransaction();
session.save(p);
session.getTransaction().commit();
session.close();


data source in my spring xml config :
all my spring xml config here https://pastebin.com/RZPr1GKL



<bean name="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/yassir" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>


<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>tp.entities.Personne</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>


<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<tx:annotation-driven />


the error :
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl.java:3450)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1418)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1414)
...










share|improve this question























  • 1. Since you're using Hibernate, you should use a HibernateTransactionManager. 2. Since you're using annotation-based transactions, the bean method that is supposed to be transactional should be annotated with @Transactional. docs.spring.io/spring/docs/current/spring-framework-reference/…

    – JB Nizet
    Dec 28 '18 at 16:33











  • I added the annotation @Transactional public void savePersonne(Personne p) { Session session = factory.getCurrentSession(); session.saveOrUpdate(p); } but the problem is still there

    – hamwac5
    Dec 28 '18 at 16:42











  • Well, apparently you read the second part of my comment, but not the first one.

    – JB Nizet
    Dec 28 '18 at 16:42











  • how to use a HibernateTransactionManager. ?

    – hamwac5
    Dec 28 '18 at 16:58











  • As shown in the link I posted in my first comment.

    – JB Nizet
    Dec 28 '18 at 16:58














0












0








0








when I try persist some data that I recovered from my csv file on my database using my job processor in spring batch this error appear in the console , for my dao i'm using hibernate



I already tried 2 methode but the same probleme !



first :



    Session session = factory.getCurrentSession();
session.saveOrUpdate(p);


second :



    Session session = factory.openSession();
session.beginTransaction();
session.save(p);
session.getTransaction().commit();
session.close();


data source in my spring xml config :
all my spring xml config here https://pastebin.com/RZPr1GKL



<bean name="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/yassir" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>


<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>tp.entities.Personne</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>


<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<tx:annotation-driven />


the error :
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl.java:3450)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1418)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1414)
...










share|improve this question














when I try persist some data that I recovered from my csv file on my database using my job processor in spring batch this error appear in the console , for my dao i'm using hibernate



I already tried 2 methode but the same probleme !



first :



    Session session = factory.getCurrentSession();
session.saveOrUpdate(p);


second :



    Session session = factory.openSession();
session.beginTransaction();
session.save(p);
session.getTransaction().commit();
session.close();


data source in my spring xml config :
all my spring xml config here https://pastebin.com/RZPr1GKL



<bean name="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/yassir" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>


<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>tp.entities.Personne</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>


<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<tx:annotation-driven />


the error :
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl.java:3450)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1418)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1414)
...







spring spring-batch






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 28 '18 at 16:26









hamwac5hamwac5

154




154













  • 1. Since you're using Hibernate, you should use a HibernateTransactionManager. 2. Since you're using annotation-based transactions, the bean method that is supposed to be transactional should be annotated with @Transactional. docs.spring.io/spring/docs/current/spring-framework-reference/…

    – JB Nizet
    Dec 28 '18 at 16:33











  • I added the annotation @Transactional public void savePersonne(Personne p) { Session session = factory.getCurrentSession(); session.saveOrUpdate(p); } but the problem is still there

    – hamwac5
    Dec 28 '18 at 16:42











  • Well, apparently you read the second part of my comment, but not the first one.

    – JB Nizet
    Dec 28 '18 at 16:42











  • how to use a HibernateTransactionManager. ?

    – hamwac5
    Dec 28 '18 at 16:58











  • As shown in the link I posted in my first comment.

    – JB Nizet
    Dec 28 '18 at 16:58



















  • 1. Since you're using Hibernate, you should use a HibernateTransactionManager. 2. Since you're using annotation-based transactions, the bean method that is supposed to be transactional should be annotated with @Transactional. docs.spring.io/spring/docs/current/spring-framework-reference/…

    – JB Nizet
    Dec 28 '18 at 16:33











  • I added the annotation @Transactional public void savePersonne(Personne p) { Session session = factory.getCurrentSession(); session.saveOrUpdate(p); } but the problem is still there

    – hamwac5
    Dec 28 '18 at 16:42











  • Well, apparently you read the second part of my comment, but not the first one.

    – JB Nizet
    Dec 28 '18 at 16:42











  • how to use a HibernateTransactionManager. ?

    – hamwac5
    Dec 28 '18 at 16:58











  • As shown in the link I posted in my first comment.

    – JB Nizet
    Dec 28 '18 at 16:58

















1. Since you're using Hibernate, you should use a HibernateTransactionManager. 2. Since you're using annotation-based transactions, the bean method that is supposed to be transactional should be annotated with @Transactional. docs.spring.io/spring/docs/current/spring-framework-reference/…

– JB Nizet
Dec 28 '18 at 16:33





1. Since you're using Hibernate, you should use a HibernateTransactionManager. 2. Since you're using annotation-based transactions, the bean method that is supposed to be transactional should be annotated with @Transactional. docs.spring.io/spring/docs/current/spring-framework-reference/…

– JB Nizet
Dec 28 '18 at 16:33













I added the annotation @Transactional public void savePersonne(Personne p) { Session session = factory.getCurrentSession(); session.saveOrUpdate(p); } but the problem is still there

– hamwac5
Dec 28 '18 at 16:42





I added the annotation @Transactional public void savePersonne(Personne p) { Session session = factory.getCurrentSession(); session.saveOrUpdate(p); } but the problem is still there

– hamwac5
Dec 28 '18 at 16:42













Well, apparently you read the second part of my comment, but not the first one.

– JB Nizet
Dec 28 '18 at 16:42





Well, apparently you read the second part of my comment, but not the first one.

– JB Nizet
Dec 28 '18 at 16:42













how to use a HibernateTransactionManager. ?

– hamwac5
Dec 28 '18 at 16:58





how to use a HibernateTransactionManager. ?

– hamwac5
Dec 28 '18 at 16:58













As shown in the link I posted in my first comment.

– JB Nizet
Dec 28 '18 at 16:58





As shown in the link I posted in my first comment.

– JB Nizet
Dec 28 '18 at 16:58












1 Answer
1






active

oldest

votes


















1














You configured Spring Batch to use a DataSourceTransactionManager to drive transactions. This transaction manager knows nothing about your Hibernate context. You need to use the HibernateTransactionManager to make the Hibernate Session in your writer participate in Spring Batch managed transactions.



I would also recommend to use the HibernateItemWriter instead of creating a custom writer (PersonneWriter) and manually creating the session and managing transactions.



A similar question/answer can be found here: Spring Batch JpaItemWriter vs HibernateItemWriter and why HibernateTransactionManager is needed when HibernateItemWriter is used



Hope this helps.






share|improve this answer
























  • thanks when i'm using HibernateTransactionManager it work : <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="dataSource" ref="dataSource" /> <property name="sessionFactory" ref="sessionFactory" /> </bean>

    – hamwac5
    Dec 28 '18 at 21:16











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53961468%2fhow-to-fix-no-transaction-is-in-progress-in-spring-batch%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You configured Spring Batch to use a DataSourceTransactionManager to drive transactions. This transaction manager knows nothing about your Hibernate context. You need to use the HibernateTransactionManager to make the Hibernate Session in your writer participate in Spring Batch managed transactions.



I would also recommend to use the HibernateItemWriter instead of creating a custom writer (PersonneWriter) and manually creating the session and managing transactions.



A similar question/answer can be found here: Spring Batch JpaItemWriter vs HibernateItemWriter and why HibernateTransactionManager is needed when HibernateItemWriter is used



Hope this helps.






share|improve this answer
























  • thanks when i'm using HibernateTransactionManager it work : <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="dataSource" ref="dataSource" /> <property name="sessionFactory" ref="sessionFactory" /> </bean>

    – hamwac5
    Dec 28 '18 at 21:16
















1














You configured Spring Batch to use a DataSourceTransactionManager to drive transactions. This transaction manager knows nothing about your Hibernate context. You need to use the HibernateTransactionManager to make the Hibernate Session in your writer participate in Spring Batch managed transactions.



I would also recommend to use the HibernateItemWriter instead of creating a custom writer (PersonneWriter) and manually creating the session and managing transactions.



A similar question/answer can be found here: Spring Batch JpaItemWriter vs HibernateItemWriter and why HibernateTransactionManager is needed when HibernateItemWriter is used



Hope this helps.






share|improve this answer
























  • thanks when i'm using HibernateTransactionManager it work : <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="dataSource" ref="dataSource" /> <property name="sessionFactory" ref="sessionFactory" /> </bean>

    – hamwac5
    Dec 28 '18 at 21:16














1












1








1







You configured Spring Batch to use a DataSourceTransactionManager to drive transactions. This transaction manager knows nothing about your Hibernate context. You need to use the HibernateTransactionManager to make the Hibernate Session in your writer participate in Spring Batch managed transactions.



I would also recommend to use the HibernateItemWriter instead of creating a custom writer (PersonneWriter) and manually creating the session and managing transactions.



A similar question/answer can be found here: Spring Batch JpaItemWriter vs HibernateItemWriter and why HibernateTransactionManager is needed when HibernateItemWriter is used



Hope this helps.






share|improve this answer













You configured Spring Batch to use a DataSourceTransactionManager to drive transactions. This transaction manager knows nothing about your Hibernate context. You need to use the HibernateTransactionManager to make the Hibernate Session in your writer participate in Spring Batch managed transactions.



I would also recommend to use the HibernateItemWriter instead of creating a custom writer (PersonneWriter) and manually creating the session and managing transactions.



A similar question/answer can be found here: Spring Batch JpaItemWriter vs HibernateItemWriter and why HibernateTransactionManager is needed when HibernateItemWriter is used



Hope this helps.







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 28 '18 at 21:01









Mahmoud Ben HassineMahmoud Ben Hassine

3,9951714




3,9951714













  • thanks when i'm using HibernateTransactionManager it work : <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="dataSource" ref="dataSource" /> <property name="sessionFactory" ref="sessionFactory" /> </bean>

    – hamwac5
    Dec 28 '18 at 21:16



















  • thanks when i'm using HibernateTransactionManager it work : <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="dataSource" ref="dataSource" /> <property name="sessionFactory" ref="sessionFactory" /> </bean>

    – hamwac5
    Dec 28 '18 at 21:16

















thanks when i'm using HibernateTransactionManager it work : <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="dataSource" ref="dataSource" /> <property name="sessionFactory" ref="sessionFactory" /> </bean>

– hamwac5
Dec 28 '18 at 21:16





thanks when i'm using HibernateTransactionManager it work : <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="dataSource" ref="dataSource" /> <property name="sessionFactory" ref="sessionFactory" /> </bean>

– hamwac5
Dec 28 '18 at 21:16


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53961468%2fhow-to-fix-no-transaction-is-in-progress-in-spring-batch%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas