DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Spring Boot: How To Use Java Persistence Query Language (JPQL)
  • How To Build Web Service Using Spring Boot 2.x
  • How To Build Self-Hosted RSS Feed Reader Using Spring Boot and Redis

Trending

  • Beyond Microservices: The Emerging Post-Monolith Architecture for 2025
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Securing Parquet Files: Vulnerabilities, Mitigations, and Validation
  • How Clojure Shapes Teams and Products
  1. DZone
  2. Coding
  3. Frameworks
  4. Using HA-JDBC with Spring Boot

Using HA-JDBC with Spring Boot

This is a really simple way to provide high-availability with failover and load balancing to any Java backend using JDBC and Spring Boot .

By 
Lieven Doclo user avatar
Lieven Doclo
·
Jul. 02, 15 · Code Snippet
Likes (1)
Comment
Save
Tweet
Share
22.6K Views

Join the DZone community and get the full member experience.

Join For Free

Sometimes you want a bit more resilience in your application and not depend on a single database instance. There are different ways you can do this, but recently I’ve come across a really simple way of enabling failover and load-balancing to any Java backend using JDBC. The library is called HA-JDBC and allows you to transparently route calls to multiple datasources. It will automatically replicate all write calls (using a master-slave mechanism) and load-balance all read calls. If a datasource gets dropped, it can also rebuild a datasource when it gets back up.

Be mindful that you always need to use the same username for all datasources. With this configuration, by default it will check every minute whether any disabled databases (due to connection failures) can be enabled again and will resync those databases.

This is a really simple way to provide high-availability with fail-over and load balancing.

import net.sf.hajdbc.SimpleDatabaseClusterConfigurationFactory
import net.sf.hajdbc.SynchronizationStrategy
import net.sf.hajdbc.balancer.BalancerFactory
import net.sf.hajdbc.balancer.random.RandomBalancerFactory
import net.sf.hajdbc.balancer.roundrobin.RoundRobinBalancerFactory
import net.sf.hajdbc.balancer.simple.SimpleBalancerFactory
import net.sf.hajdbc.cache.DatabaseMetaDataCacheFactory
import net.sf.hajdbc.cache.eager.EagerDatabaseMetaDataCacheFactory
import net.sf.hajdbc.cache.eager.SharedEagerDatabaseMetaDataCacheFactory
import net.sf.hajdbc.cache.lazy.LazyDatabaseMetaDataCacheFactory
import net.sf.hajdbc.cache.lazy.SharedLazyDatabaseMetaDataCacheFactory
import net.sf.hajdbc.cache.simple.SimpleDatabaseMetaDataCacheFactory
import net.sf.hajdbc.sql.Driver
import net.sf.hajdbc.sql.DriverDatabase
import net.sf.hajdbc.sql.DriverDatabaseClusterConfiguration
import net.sf.hajdbc.state.StateManagerFactory
import net.sf.hajdbc.state.bdb.BerkeleyDBStateManagerFactory
import net.sf.hajdbc.state.simple.SimpleStateManagerFactory
import net.sf.hajdbc.state.sql.SQLStateManagerFactory
import net.sf.hajdbc.state.sqlite.SQLiteStateManagerFactory
import net.sf.hajdbc.sync.*
import net.sf.hajdbc.util.concurrent.cron.CronExpression
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration

import javax.annotation.PostConstruct

@ConfigurationProperties(prefix="hajdbc")
@ConditionalOnClass(Driver)
@Configuration
class HaJdbcConfiguration {
    List<DriverDatabase> driverDatabases;
    String clusterName = "default"
    String cronExpression = "0 0/1 * 1/1 * ? *"
    String balancerFactory = "round-robin"
    String defaultSynchronizationStrategy = "full"
    String databaseMetaDataCacheFactory = "shared-eager"
    String stateManagerFactory = "simple"
    String stateManagerUrl
    String stateManagerUser
    String stateManagerPassword
    String stateManagerLocation
    boolean identityColumnDetectionEnabled = true
    boolean sequenceDetectionEnabled = true

    @PostConstruct
    def register() {
        DriverDatabaseClusterConfiguration config = new DriverDatabaseClusterConfiguration();
        config.setDatabases(driverDatabases);
        config.databaseMetaDataCacheFactory = DatabaseMetaDataCacheChoice.fromId(databaseMetaDataCacheFactory)
        config.balancerFactory = BalancerChoice.fromId(balancerFactory)
        config.stateManagerFactory = StateManagerChoice.fromId(stateManagerFactory)
        switch(stateManagerFactory) {
            case "sql":
               def mgr = config.stateManagerFactory as SQLStateManagerFactory
                if(stateManagerUrl)
                    mgr.urlPattern = stateManagerUrl
                if(stateManagerUser)
                    mgr.user = stateManagerUser
                if(stateManagerPassword)
                    mgr.password = stateManagerPassword
                break;
            case "berkeleydb":
            case "sqlite":
                def mgr = config.stateManagerFactory as BerkeleyDBStateManagerFactory
                if(stateManagerLocation)
                    mgr.locationPattern = stateManagerLocation
                break;
        }
        config.synchronizationStrategyMap = SynchronizationStrategyChoice.idMap
        config.defaultSynchronizationStrategy = defaultSynchronizationStrategy
        config.identityColumnDetectionEnabled = identityColumnDetectionEnabled
        config.sequenceDetectionEnabled = sequenceDetectionEnabled
        config.setAutoActivationExpression(new CronExpression(cronExpression))

        Driver.setConfigurationFactory(clusterName,
                                       new SimpleDatabaseClusterConfigurationFactory<java.sql.Driver, DriverDatabase>(config));
    }

    static enum DatabaseMetaDataCacheChoice {
        SIMPLE(new SimpleDatabaseMetaDataCacheFactory()),
        LAZY(new LazyDatabaseMetaDataCacheFactory()),
        EAGER(new EagerDatabaseMetaDataCacheFactory()),
        SHARED_LAZY(new SharedLazyDatabaseMetaDataCacheFactory()),
        SHARED_EAGER(new SharedEagerDatabaseMetaDataCacheFactory());

        DatabaseMetaDataCacheFactory databaseMetaDataCacheFactory;

        DatabaseMetaDataCacheChoice(DatabaseMetaDataCacheFactory databaseMetaDataCacheFactory) {
            this.databaseMetaDataCacheFactory = databaseMetaDataCacheFactory
        }

        static DatabaseMetaDataCacheFactory fromId(String id) {
            values().find {
                it.databaseMetaDataCacheFactory.id == id
            }.databaseMetaDataCacheFactory
        }
    }

    static enum BalancerChoice {
        ROUND_ROBIN(new RoundRobinBalancerFactory()),
        RANDOM(new RandomBalancerFactory()),
        SIMPLE(new SimpleBalancerFactory());

        BalancerFactory balancerFactory;

        BalancerChoice(BalancerFactory balancerFactory) {
            this.balancerFactory = balancerFactory
        }

        static BalancerFactory fromId(String id) {
            values().find {
                it.balancerFactory.id == id
            }.balancerFactory
        }
    }

    static enum SynchronizationStrategyChoice {
        FULL(new FullSynchronizationStrategy()),
        DUMP_RESTORE(new DumpRestoreSynchronizationStrategy()),
        DIFF(new DifferentialSynchronizationStrategy()),
        FASTDIFF(new FastDifferentialSynchronizationStrategy()),
        PER_TABLE_FULL(new PerTableSynchronizationStrategy(new FullSynchronizationStrategy())),
        PER_TABLE_DIFF(new PerTableSynchronizationStrategy(new DifferentialSynchronizationStrategy())),
        PASSIVE(new PassiveSynchronizationStrategy());

        SynchronizationStrategy synchronizationStrategy;

        SynchronizationStrategyChoice(SynchronizationStrategy synchronizationStrategy) {
            this.synchronizationStrategy = synchronizationStrategy
        }

        static SynchronizationStrategy fromId(String id) {
            values().find {
                it.synchronizationStrategy.id == id
            }.synchronizationStrategy
        }

        static Map<String, SynchronizationStrategy> getIdMap() {
            return values().collectEntries {
                [(it.synchronizationStrategy.id):it.synchronizationStrategy]
            }
        }
    }

    static enum StateManagerChoice {
        SIMPLE(new SimpleStateManagerFactory()),
        BERKELEYDB(new BerkeleyDBStateManagerFactory()),
        SQLITE(new SQLiteStateManagerFactory()),
        SQL(new SQLStateManagerFactory());

        private StateManagerFactory stateManagerFactory;

        StateManagerChoice(StateManagerFactory stateManagerFactory) {
            this.stateManagerFactory = stateManagerFactory
        }

        static StateManagerFactory fromId(String id) {
            values().find {
                it.stateManagerFactory.id == id
            }.stateManagerFactory
        }

    }
}

When your Spring Boot application picks up this class, you can configure your Spring Boot application to use HA-JDBC by configuring its databases and the standard datasource Spring Boot provides when including JDBC support. For example, this configuration creates a datasource that replicates between two MySQL databases.

spring.datasource.url=jdbc:ha-jdbc:default
spring.datasource.username=root
spring.datasource.driver-class-name=net.sf.hajdbc.sql.Driver

hajdbc.driverDatabases[0].id=db1
hajdbc.driverDatabases[0].location=jdbc:mysql://localhost/hatest1
hajdbc.driverDatabases[0].user=root
hajdbc.driverDatabases[1].id=db2
hajdbc.driverDatabases[1].location=jdbc:mysql://localhost/hatest2
hajdbc.driverDatabases[1].user=root

Be mindful that you always need to use the same username for all datasources. With this configuration, by default it will check every minute whether any disabled databases (due to connection failures) can be enabled again and will resync those databases.

This is a really simple way to provide high-availability with fail-over and load balancing.


Spring Framework Spring Boot Database

Published at DZone with permission of Lieven Doclo, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Spring Boot: How To Use Java Persistence Query Language (JPQL)
  • How To Build Web Service Using Spring Boot 2.x
  • How To Build Self-Hosted RSS Feed Reader Using Spring Boot and Redis

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!