CraigR8806 CraigR8806 - 1 year ago 125
Java Question

Spring utilizing Mongo implementation over JPA

I am fairly new to the Spring Framework and have been having some trouble setting up the project I am currently working on. I need to be able to connect to two different databases one being MongoDB and the other being MSSQL. I am using JPA to connect to the MSSQL.

The problem that I am encountering is that it appears to be trying to make calls to the Mongo database when I want it to make calls to the MSSQL and I'm not really sure how to tell it what to read from. I have seen the posts advising to use the

annotation to direct it to the correct implementation, but I don't think that that will work for my case.

public class SoftwareEndpoint {

SoftwareRepository repo;

********************************MSSQL calls****************************************
String getAllSoftware(){

List<Software> allSoftware = (List<Software>) repo.findAll();
//rest of method and class

Above shows a snippet of my controller class that has an instance of my SoftwareRepository. I also print to the out stream before and after the db call.

The out stream only shows "Here1", goes on to print out this line:

2016-10-04 07:35:39.810 INFO 4236 --- [nio-8080-exec-2] org.mongodb.driver.cluster : No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by { Connection refused: connect}}]}. Waiting for 30000 ms before timing out

and then throws an exception on timeout.

I do not have a mongo instance running locally, however there will be on where the application is being deployed, but I don't believe that this is the problem because on hitting that endpoint, it shouldn't be making a call to the Mongo database, it should be trying to reach out to MSSQL.

TLDR: How do I specify which database implementation for Spring to use for a specific repository or database call?

Answer Source

You can connect to different databases in spring based on the configuration in context.

The below code is for connecting to MySql and Mongo DB. You can substitute MySql with MSSQL provided you have the JDBC for it. Check for what the properties for JDBC connection mean.

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="mySqldataSource" /> <!-- Change the datasource to MSSQL-->

    <bean id="mySqldataSource" class="org.apache.commons.dbcp.BasicDataSource"
        <property name="removeAbandoned">
        <property name="removeAbandonedTimeout">
        <property name="driverClassName">
        <property name="url">
        <property name="username">
        <property name="password">
        <property name="maxIdle"> 
        <property name="maxActive"> 
        <property name="maxWait">
        <property name="testOnBorrow">
        <property name="testWhileIdle">
        <property name="timeBetweenEvictionRunsMillis">
        <property name="minEvictableIdleTimeMillis">
        <property name="numTestsPerEvictionRun">
        <property name="defaultTransactionIsolation" value="1" />
        <property name="poolPreparedStatements" value="true" />
        <property name="maxOpenPreparedStatements" value="1" />

    <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"></bean>

Below is for connecting to mongodb

    <mongo:db-factory dbname="mongoDbName" host="mongoServer" port="mongoPort"/>

    <bean id="mongoTemplate" class="">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    <mongo:repositories base-package="com.test.repoPackage"/> <!-- Package containing the mongo repository interfaces -->

Now you can use the repositories provided by spring.

EDIT 1: Suppose name of config is In the above example for the properties dbname, host and port in mongo:db-factory, you would want the values to be configured in So lets name them below:

mongoServer =
mongoPort = 27017
mongoDb = testDb

Now the context file needs to be modified to import the this is done as below in the context file:

<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
        <property name="locations" >

The bean mongo:db-factory would now look like:

<mongo:db-factory dbname="${mongoDb}" host="${mongoServer}" port="${mongoPort}"/>

Notice that the "keys" from config (dbname, host and port) are represented insde ${}. This will replace with values in config for the keys.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download