Skip to content

Kode Java - Learn Java by Examples

  • Partners

MaxConnections

How do I get the maximum number of concurrent connections?

Last update: May 31, 2024 by Wayan

This example shows you how to get the maximum number of concurrent connections to a database that are possible.
To get this information, we use the DatabaseMetaData.getMaxConnections() method call. If the return value is zero, it means that there is no limit or the limit is unknown.

package org.kodejava.jdbc;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.sql.DriverManager;

public class MaxConnections {
    private static final String URL = "jdbc:mysql://localhost/kodejava";
    private static final String USERNAME = "kodejava";
    private static final String PASSWORD = "s3cr*t";

    public static void main(String[] args) {
        try (Connection connection =
                     DriverManager.getConnection(URL, USERNAME, PASSWORD)) {

            // Get database meta data.
            DatabaseMetaData metaData = connection.getMetaData();

            // Retrieves the maximum number of concurrent
            // connections to this database that are possible.
            // A result of zero means that there is no limit or
            // the limit is not known.
            int max = metaData.getMaxConnections();
            System.out.println("Max concurrent connections: " + max);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.4.0</version>
</dependency>

Maven Central

Categories JDBC Tags DatabaseMetaData, JDBC, MaxConnections, MetaData Leave a comment

How do I get the max concurrent connection to a database?

Last update: May 31, 2024 by Wayan
package org.kodejava.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

public class MaxConcurrentConnection {
    private static final String URL = "jdbc:mysql://localhost/kodejava";
    private static final String USERNAME = "kodejava";
    private static final String PASSWORD = "s3cr*t";

    public static void main(String[] args) {
        try (Connection connection =
                     DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
            DatabaseMetaData metadata = connection.getMetaData();

            // Get the information of maximum concurrent connection to the
            // database. The maximum number of active connections possible
            // at one time; a result of zero means that there is no limit
            // or the limit is not known.
            int maxConnection = metadata.getMaxConnections();
            System.out.println("Maximum Connection = " + maxConnection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

The result of the code snippet above:

Maximum Connection = 0

Maven Dependencies

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.4.0</version>
</dependency>

Maven Central

Categories JDBC Tags Connection, DatabaseMetaData, JDBC, MaxConnections Leave a comment

  • Java
    • 2D API
    • Applet
    • AWT
    • Basic
    • Beans
    • Concurrency
    • Core API
    • Cryptography
    • Generics
    • HttpClient
    • IO
    • Java Date Time API
    • JDBC
    • JNDI
    • Lang Package
    • Logging
    • Mail
    • Management
    • Math
    • Net Package
    • Networking
    • Print Service
    • Reflection
    • Regex
    • Scripting
    • Security
    • Sound API
    • Stream API
    • Swing
    • Text Package
    • Util Package
    • XML
    • Zip and GZIP
    • Java 8
    • Java 9
    • Java 10
    • Java 11
    • Java 12
    • Java 14
    • Java 15
    • Java 16
    • Java 17
    • Java 21
    • Java 24
    • Java 25
  • Spring
    • Spring AI
    • Spring Boot
    • Spring Core
    • Spring Data JPA
    • Spring JDBC
    • Spring JMS
    • Spring MVC
    • Spring Security
  • Persistence
    • Hibernate
    • Java Persistence API
    • MyBatis
  • Servlet
    • JSP
    • Servlet
    • Taglib
  • Other Libraries
    • Apache Commons
    • Apache HttpComponents
    • Apache POI
    • E-iceblue Spire
    • Google Gson
    • iText PDF
    • Jackson
    • Jasypt
    • JDOM
    • Joda-Time
    • jPOS
    • JSch
    • JSON-Java
    • Project Lombok
    • ZK Framework
  • Android
    • Android Misc
    • Android Programming
  • Game Programming
    • Java 2D Game Programming
  • Patterns
    • Creational Patterns
  • Swing Loops
    • Animation
  • JVM Languages
    • Kotlin
  • Databases
    • Learning SQL
    • Microsoft Access
    • MongoDB
    • MySQL
  • Tools
    • Docker
    • Eclipse
    • Gradle
    • IntelliJ IDEA
    • JUnit
    • Maven
    • OS X
    • PlantUML
    • Ubuntu
    • UML
  • Partners
    • Buku Java ID

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

© 2026 Kode Java - Learn Java by Examples • Built with ♡ and ☕ in Bali, Indonesia