Transactions with multiple datasources in Spring












0















I have 2 classes for working with JDBC and Sqlite. These classes use abstract class to extend:



package atm.implementations;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;

public abstract class AbstractDAO {

NamedParameterJdbcTemplate jdbcTemplate;

@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
}


First class:



package atm.implementations;

import atm.dao.SQLiteDAO;
import atm.objects.Bank;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.ResultSet;
import java.sql.SQLException;

@Component("bankDAO")
public class BankDAO extends AbstractDAO implements SQLiteDAO<Bank> {


@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE)
public void updateRecord(Bank object) {

String sqlUpdate = "update bank set account_value = :value where id = :id";
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("value", object.getAccountValue());
params.addValue("id", object.getId());

jdbcTemplate.update(sqlUpdate, params);
}
}


And second:



package atm.implementations;

import atm.dao.TranDAO;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Component("transDAO")
public class TranDAOImpl extends AbstractDAO implements TranDAO {

@Transactional(propagation = Propagation.MANDATORY, isolation = Isolation.SERIALIZABLE)
public void insert(String desc) {

String sqlInsert = "insert into transactions_list (description) values (:descr)";
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("descr", desc);

jdbcTemplate.update(sqlInsert, params);
}
}


And here is context.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:component-scan base-package="atm.*"/>
<aop:aspectj-autoproxy proxy-target-class="true"/>


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value=""/>
<property name="password" value=""/>
<property name="url" value="jdbc:sqlite:atm.db"/>
<property name="driverClassName" value="org.sqlite.JDBC"/>
</bean>

<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />

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


</beans>


I need to make methods in two these classes Transactional, but Datasource is different in each instance of classes. Now i have error - No existing transaction found for transaction marked with propagation 'mandatory'.



First class updates database with new account parameters and second class just insert text in separate table about this transaction. If transaction failed, second class shouldn't insert any information.



I really don't want to join all code in one class, i'd like to keep it separated.



Is any method to use ONE SINGLE datasource for all classes??










share|improve this question



























    0















    I have 2 classes for working with JDBC and Sqlite. These classes use abstract class to extend:



    package atm.implementations;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

    import javax.sql.DataSource;

    public abstract class AbstractDAO {

    NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    public void setDataSource(DataSource dataSource) {
    this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
    }
    }


    First class:



    package atm.implementations;

    import atm.dao.SQLiteDAO;
    import atm.objects.Bank;
    import org.springframework.jdbc.core.RowMapper;
    import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
    import org.springframework.stereotype.Component;
    import org.springframework.transaction.annotation.Isolation;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;

    import java.sql.ResultSet;
    import java.sql.SQLException;

    @Component("bankDAO")
    public class BankDAO extends AbstractDAO implements SQLiteDAO<Bank> {


    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE)
    public void updateRecord(Bank object) {

    String sqlUpdate = "update bank set account_value = :value where id = :id";
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("value", object.getAccountValue());
    params.addValue("id", object.getId());

    jdbcTemplate.update(sqlUpdate, params);
    }
    }


    And second:



    package atm.implementations;

    import atm.dao.TranDAO;
    import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
    import org.springframework.stereotype.Component;
    import org.springframework.transaction.annotation.Isolation;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;

    @Component("transDAO")
    public class TranDAOImpl extends AbstractDAO implements TranDAO {

    @Transactional(propagation = Propagation.MANDATORY, isolation = Isolation.SERIALIZABLE)
    public void insert(String desc) {

    String sqlInsert = "insert into transactions_list (description) values (:descr)";
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("descr", desc);

    jdbcTemplate.update(sqlInsert, params);
    }
    }


    And here is context.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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 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/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="atm.*"/>
    <aop:aspectj-autoproxy proxy-target-class="true"/>


    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="username" value=""/>
    <property name="password" value=""/>
    <property name="url" value="jdbc:sqlite:atm.db"/>
    <property name="driverClassName" value="org.sqlite.JDBC"/>
    </bean>

    <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />

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


    </beans>


    I need to make methods in two these classes Transactional, but Datasource is different in each instance of classes. Now i have error - No existing transaction found for transaction marked with propagation 'mandatory'.



    First class updates database with new account parameters and second class just insert text in separate table about this transaction. If transaction failed, second class shouldn't insert any information.



    I really don't want to join all code in one class, i'd like to keep it separated.



    Is any method to use ONE SINGLE datasource for all classes??










    share|improve this question

























      0












      0








      0








      I have 2 classes for working with JDBC and Sqlite. These classes use abstract class to extend:



      package atm.implementations;

      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

      import javax.sql.DataSource;

      public abstract class AbstractDAO {

      NamedParameterJdbcTemplate jdbcTemplate;

      @Autowired
      public void setDataSource(DataSource dataSource) {
      this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
      }
      }


      First class:



      package atm.implementations;

      import atm.dao.SQLiteDAO;
      import atm.objects.Bank;
      import org.springframework.jdbc.core.RowMapper;
      import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
      import org.springframework.stereotype.Component;
      import org.springframework.transaction.annotation.Isolation;
      import org.springframework.transaction.annotation.Propagation;
      import org.springframework.transaction.annotation.Transactional;

      import java.sql.ResultSet;
      import java.sql.SQLException;

      @Component("bankDAO")
      public class BankDAO extends AbstractDAO implements SQLiteDAO<Bank> {


      @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE)
      public void updateRecord(Bank object) {

      String sqlUpdate = "update bank set account_value = :value where id = :id";
      MapSqlParameterSource params = new MapSqlParameterSource();
      params.addValue("value", object.getAccountValue());
      params.addValue("id", object.getId());

      jdbcTemplate.update(sqlUpdate, params);
      }
      }


      And second:



      package atm.implementations;

      import atm.dao.TranDAO;
      import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
      import org.springframework.stereotype.Component;
      import org.springframework.transaction.annotation.Isolation;
      import org.springframework.transaction.annotation.Propagation;
      import org.springframework.transaction.annotation.Transactional;

      @Component("transDAO")
      public class TranDAOImpl extends AbstractDAO implements TranDAO {

      @Transactional(propagation = Propagation.MANDATORY, isolation = Isolation.SERIALIZABLE)
      public void insert(String desc) {

      String sqlInsert = "insert into transactions_list (description) values (:descr)";
      MapSqlParameterSource params = new MapSqlParameterSource();
      params.addValue("descr", desc);

      jdbcTemplate.update(sqlInsert, params);
      }
      }


      And here is context.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:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop" 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/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

      <context:component-scan base-package="atm.*"/>
      <aop:aspectj-autoproxy proxy-target-class="true"/>


      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="username" value=""/>
      <property name="password" value=""/>
      <property name="url" value="jdbc:sqlite:atm.db"/>
      <property name="driverClassName" value="org.sqlite.JDBC"/>
      </bean>

      <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />

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


      </beans>


      I need to make methods in two these classes Transactional, but Datasource is different in each instance of classes. Now i have error - No existing transaction found for transaction marked with propagation 'mandatory'.



      First class updates database with new account parameters and second class just insert text in separate table about this transaction. If transaction failed, second class shouldn't insert any information.



      I really don't want to join all code in one class, i'd like to keep it separated.



      Is any method to use ONE SINGLE datasource for all classes??










      share|improve this question














      I have 2 classes for working with JDBC and Sqlite. These classes use abstract class to extend:



      package atm.implementations;

      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

      import javax.sql.DataSource;

      public abstract class AbstractDAO {

      NamedParameterJdbcTemplate jdbcTemplate;

      @Autowired
      public void setDataSource(DataSource dataSource) {
      this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
      }
      }


      First class:



      package atm.implementations;

      import atm.dao.SQLiteDAO;
      import atm.objects.Bank;
      import org.springframework.jdbc.core.RowMapper;
      import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
      import org.springframework.stereotype.Component;
      import org.springframework.transaction.annotation.Isolation;
      import org.springframework.transaction.annotation.Propagation;
      import org.springframework.transaction.annotation.Transactional;

      import java.sql.ResultSet;
      import java.sql.SQLException;

      @Component("bankDAO")
      public class BankDAO extends AbstractDAO implements SQLiteDAO<Bank> {


      @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE)
      public void updateRecord(Bank object) {

      String sqlUpdate = "update bank set account_value = :value where id = :id";
      MapSqlParameterSource params = new MapSqlParameterSource();
      params.addValue("value", object.getAccountValue());
      params.addValue("id", object.getId());

      jdbcTemplate.update(sqlUpdate, params);
      }
      }


      And second:



      package atm.implementations;

      import atm.dao.TranDAO;
      import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
      import org.springframework.stereotype.Component;
      import org.springframework.transaction.annotation.Isolation;
      import org.springframework.transaction.annotation.Propagation;
      import org.springframework.transaction.annotation.Transactional;

      @Component("transDAO")
      public class TranDAOImpl extends AbstractDAO implements TranDAO {

      @Transactional(propagation = Propagation.MANDATORY, isolation = Isolation.SERIALIZABLE)
      public void insert(String desc) {

      String sqlInsert = "insert into transactions_list (description) values (:descr)";
      MapSqlParameterSource params = new MapSqlParameterSource();
      params.addValue("descr", desc);

      jdbcTemplate.update(sqlInsert, params);
      }
      }


      And here is context.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:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop" 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/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

      <context:component-scan base-package="atm.*"/>
      <aop:aspectj-autoproxy proxy-target-class="true"/>


      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="username" value=""/>
      <property name="password" value=""/>
      <property name="url" value="jdbc:sqlite:atm.db"/>
      <property name="driverClassName" value="org.sqlite.JDBC"/>
      </bean>

      <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />

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


      </beans>


      I need to make methods in two these classes Transactional, but Datasource is different in each instance of classes. Now i have error - No existing transaction found for transaction marked with propagation 'mandatory'.



      First class updates database with new account parameters and second class just insert text in separate table about this transaction. If transaction failed, second class shouldn't insert any information.



      I really don't want to join all code in one class, i'd like to keep it separated.



      Is any method to use ONE SINGLE datasource for all classes??







      java spring jdbc transactions






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 20:13









      artart

      85




      85
























          1 Answer
          1






          active

          oldest

          votes


















          0














          TranDAOImpl#insert is marked with MANDATORY-Transaction-Propagation. The meaning if this is, that the caller must have an active transaction. I suspect that you get the exception when calling this method?



          So the caller itself should be annotated by "REQUIRED" or "REQUIRES_NEW". I don't think that your problem is related to using different Datasources. If your Spring-wiring is done correctly, the transaction-Manager should handle that in a safe way.






          share|improve this answer























            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%2f53471482%2ftransactions-with-multiple-datasources-in-spring%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









            0














            TranDAOImpl#insert is marked with MANDATORY-Transaction-Propagation. The meaning if this is, that the caller must have an active transaction. I suspect that you get the exception when calling this method?



            So the caller itself should be annotated by "REQUIRED" or "REQUIRES_NEW". I don't think that your problem is related to using different Datasources. If your Spring-wiring is done correctly, the transaction-Manager should handle that in a safe way.






            share|improve this answer




























              0














              TranDAOImpl#insert is marked with MANDATORY-Transaction-Propagation. The meaning if this is, that the caller must have an active transaction. I suspect that you get the exception when calling this method?



              So the caller itself should be annotated by "REQUIRED" or "REQUIRES_NEW". I don't think that your problem is related to using different Datasources. If your Spring-wiring is done correctly, the transaction-Manager should handle that in a safe way.






              share|improve this answer


























                0












                0








                0







                TranDAOImpl#insert is marked with MANDATORY-Transaction-Propagation. The meaning if this is, that the caller must have an active transaction. I suspect that you get the exception when calling this method?



                So the caller itself should be annotated by "REQUIRED" or "REQUIRES_NEW". I don't think that your problem is related to using different Datasources. If your Spring-wiring is done correctly, the transaction-Manager should handle that in a safe way.






                share|improve this answer













                TranDAOImpl#insert is marked with MANDATORY-Transaction-Propagation. The meaning if this is, that the caller must have an active transaction. I suspect that you get the exception when calling this method?



                So the caller itself should be annotated by "REQUIRED" or "REQUIRES_NEW". I don't think that your problem is related to using different Datasources. If your Spring-wiring is done correctly, the transaction-Manager should handle that in a safe way.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 27 '18 at 16:10









                aschoerkaschoerk

                1,3661524




                1,3661524
































                    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%2f53471482%2ftransactions-with-multiple-datasources-in-spring%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

                    Wiesbaden

                    Marschland

                    Dieringhausen