Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6741237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:42:03+00:00 2026-05-26T11:42:03+00:00

How to use multiple datasouce and sessionFactory during a junit runtime test? I use

  • 0

How to use multiple datasouce and sessionFactory during a junit runtime test?

I use hibernateDaoTemplate for persistence, and my structure is right down here:

I just want to get, say, products from a different db and then persist them to a different db in ProductStatServiceTest runtime.

Thanks in advance.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/dataSource.xml", "/hibernate.xml", "/applicationContext.xml"})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class})
@TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)
public abstract class GenericTransactionalUnitTest extends   AbstractTransactionalJUnit4SpringContextTests {
  private Session session;

  protected abstract SessionFactory getSessionFactory();
  ...
}

@Transactional(readOnly = true)
abstract public class ProductStatUnitTest extends GenericTransactionalUnitTest {
  @Autowired
  @Qualifier(value = "productStatSessionFactory")
  private SessionFactory sessionFactory;

  @Resource(name = "productStatDS")
  public void setDataSource(DataSource dataSource) {
    super.setDataSource(dataSource);
  }

  @Override
  protected SessionFactory getSessionFactory() {
         return sessionFactory;
  }
}

public class ProductStatServiceTest extends ProductStatUnitTest {
  @Autowired
  private ProductStatService productStatService;  

  @Test
  public List<ProductStat> testInjection() throws ParseException {
     productStatService.findById(123456); 
  }
..
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:42:03+00:00Added an answer on May 26, 2026 at 11:42 am

    2 Database => 2 session factories and datasources. This currently is not possible in your code.

    I assume you have 1 ProductStatService but your requirement is that this service will work with sessionfactoryA for the ‘select’ and sessionfactoryB for the ‘insert’.

    Hence, you will need to create (and inject) 2 instances of this service in your unit test and these of course can’t be scoped as singletons (assuming you use Spring for DI).
    You will have to rewrite some of your code, the actual sessionfactory will need to be injected into the service and not retrieved by GenericTransactionalUnitTest.getSessionFactory().

    UPDATE

    If you don’t want to create 2 DAO’s then this also an option:

    public class ProductStatDAO extends AbstractHibernateDao {
    
    @Autowired(required = false)
    @Qualifier("sessionFactoryA")
    private SessionFactory sessionFactoryA;
    
    @Autowired(required = false)
    @Qualifier("sessionFactoryB")
    private SessionFactory sessionFactoryB;
    
    //called by Spring (init-method)
    public void init() {
        if (getSessionFactory() == null) {
            if (sessionFactoryA != null) {
                setSessionFactory(sessionFactoryA);
            } else if (sessionFactoryB != null) {
                setSessionFactory(sessionFactoryB);
            } 
            // else throw exception
        }
    }
    

    }

    You will however still need to inject 2 different instances of this DAO (so scope=’prototype’) in your service.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I use multiple repositories, all located under a single root folder, how can
I'm trying to use multiple attributes in my custom tag, e.g.: <mytaglib:mytag firstname=Thadeus lastname=Jones
I ask myself how use multiple images in one. For example youtube -> http://s.ytimg.com/yt/img/master-vfl102488.png
I need to use multiple hosts under IIS for WCF. We're using wshttpbinding and
Is it possible to use multiple header types in one document? For example: header(Content-type:
I want to use multiple private keys to connect to different servers or different
In my Activity I use multiple AsyncTask classes. How to cancel AsyncTask when Activity
I am trying to use multiple textures in the same scene but no matter
What is the best way to use multiple EVAL fields in a GridView ItemTemplate?
Under what circumstances might you want to use multiple indirection (that is, a chain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.