When you have 200 concurrent users hitting a single-threaded service, you’ll see the connection pool exhaustion error: “java.sql.SQLException: Unable to acquire JDBC Connection”. This is because the default connection pool size is not sufficient to handle the load. To solve this, you need to implement a more efficient connection pool management system.
## PREREQUISITES To follow this tutorial, you need to have the following prerequisites: * Java 21 * Spring Boot 3.0 * Maven 4.0 * Gradle 8.0 The following Maven dependency is required:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
## UNDERSTANDING UNNAMED CLASSES AND INSTANCE MAIN METHODS Unnamed classes, also known as anonymous classes, are classes that are defined without a name. They are used to create instances of classes that implement interfaces or extend abstract classes. Instance main methods, on the other hand, are methods that are defined inside a class and are used to create instances of that class. The following ASCII diagram illustrates the concept of unnamed classes and instance main methods:
+---------------+ | Unnamed | | Class | +---------------+ | | v +---------------+ | Instance | | Main Method| +---------------+ | | v +---------------+ | Class | | Instance | +---------------+
The following table compares the different approaches to implementing unnamed classes and instance main methods:
| Approach | Description |
|---|---|
| Unnamed Classes | Classes defined without a name |
| Instance Main Methods | Methods defined inside a class to create instances |
## STEP-BY-STEP IMPLEMENTATION To implement unnamed classes and instance main methods, follow these steps: ### Step 1: Define the Unnamed Class Define an unnamed class that implements an interface or extends an abstract class.
public class Main { public static void main(String[] args) { // Define an unnamed class that implements an interface Runnable runnable = new Runnable() { @Override public void run() { System.out.println("Hello, World!"); } }; // Create an instance of the unnamed class Thread thread = new Thread(runnable); thread.start(); } }
Expected output:
Hello, World!
### Step 2: Define the Instance Main Method Define an instance main method inside a class to create instances of that class.
public class Main { public static void main(String[] args) { // Define an instance main method Main main = new Main(); main.instanceMainMethod(); } public void instanceMainMethod() { System.out.println("Hello, World!"); } }
Expected output:
Hello, World!
## COMPLETE WORKING EXAMPLE The following is a complete working example that demonstrates the use of unnamed classes and instance main methods:
// com.example.app.Main.java public class Main { public static void main(String[] args) { // Define an unnamed class that implements an interface Runnable runnable = new Runnable() { @Override public void run() { System.out.println("Hello, World!"); } }; // Create an instance of the unnamed class Thread thread = new Thread(runnable); thread.start(); } public void instanceMainMethod() { System.out.println("Hello, World!"); } } // com.example.app.Controller.java @RestController public class Controller { @GetMapping("/") public String index() { return "Hello, World!"; } }
To run the example, use the following curl command: “`bash curl http://localhost:8080/ “` Expected response: “`json Hello, World! “` ## COMMON MISTAKES AND HOW TO FIX THEM The following are common mistakes that can occur when implementing unnamed classes and instance main methods: ### Mistake 1: Not Closing the Connection Pool The following code does not close the connection pool:
// WRONG - causes connection pool exhaustion public class Main { public static void main(String[] args) { Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "user", "password"); // ... } }
The following error occurs:
java.sql.SQLException: Unable to acquire JDBC Connection
To fix the mistake, close the connection pool:
public class Main { public static void main(String[] args) { Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "user", "password"); try { // ... } finally { connection.close(); } } }
## PERFORMANCE AND PRODUCTION TIPS The following are performance and production tips for implementing unnamed classes and instance main methods:
Production tip: Use a connection pool to improve performance. Set the
hikari.maximum-pool-sizeproperty to 10 for a 4-core server.
Production tip: Use a thread pool to improve performance. Set the
thread.pool.sizeproperty to 10 for a 4-core server.
## TESTING To test the implementation of unnamed classes and instance main methods, use the following JUnit 5 test:
@SpringBootTest public class MainTest { @Test public void testUnnamedClass() { // Define an unnamed class that implements an interface Runnable runnable = new Runnable() { @Override public void run() { System.out.println("Hello, World!"); } }; // Create an instance of the unnamed class Thread thread = new Thread(runnable); thread.start(); // Assert that the instance is created assertNotNull(thread); } }
For more information on testing, see the Java Algorithms tutorial. ## KEY TAKEAWAYS The following are key takeaways from this tutorial: * Use unnamed classes to implement interfaces or extend abstract classes. * Use instance main methods to create instances of classes. * Use a connection pool to improve performance. * Use a thread pool to improve performance. * Test the implementation using JUnit 5 tests. For more Java tutorials, see the Java Tutorials Hub pillar page.
java-examples — Clone, Star & Contribute

Leave a Reply