I’m working on a Spring application that connects to an external Oracle database. The external connection can be intermittent, and when it’s down, I need to store the data locally. Here’s my approach:
When an insert operation (remoteDatabase.Save(newObject)) fails due to a connection issue, I catch the exception.
In the catch block, I save the object to a local SQLite database (localDatabase.Save(newObject)).
Is this a reasonable approach? Are there any best practices or alternative solutions for handling intermittent external database connections in Spring applications?
Details:
Spring framework version: [Specify your Spring version]
External database: Oracle
Local database: SQLite
Connection testing: Separate thread checks if the external connection is up
Exception handling: Catch exceptions during remoteDatabase.Save(newObject) and fallback to localDatabase.Save(newObject)
Any insights or suggestions would be greatly appreciated!
I have tried to grep the exception from the remote database by doing the Global Exception Handling but with no luck…
The exception i was trying do handle or trigger by was:
2024-06-21 14:28:15.278 --- thread_id="1" thread_name="main" thread_priority="5" --- [DEBUG] Spring test ApplicationContext cache statistics: [DefaultContextCache@171b82ea size = 1, maxSize = 32, parentContextCount = 0, hitCount = 6, missCount = 1, failureCount = 0] 2024-06-21 14:28:15.280 --- thread_id="1" thread_name="main" thread_priority="5" --- [DEBUG] Creating new transaction with name [org.springframework.data.jpa.repository.support.SimpleJpaRepository.count]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly 2024-06-21 14:28:15.280 --- thread_id="1" thread_name="main" thread_priority="5" --- [DEBUG] Opened new EntityManager [SessionImpl(1913887027<open>)] for JPA transaction 2024-06-21 14:28:15.280 --- thread_id="1" thread_name="main" thread_priority="5" --- [DEBUG] Creating new JDBC DriverManager Connection to [jdbc:oracle:thin:@localhost:1522:FREE] 2024-06-21 14:28:15.284 --- thread_id="1" thread_name="main" thread_priority="5" --- [DEBUG] Unable to acquire JDBC Connection [n/a] java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:907) ~[ojdbc10-19.22.0.0.jar:19.22.0.0.0] at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:820) ~[ojdbc10-19.22.0.0.jar:19.22.0.0.0] at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:80) ~[ojdbc10-19.22.0.0.jar:19.22.0.0.0] at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:820) ~[ojdbc10-19.22.0.0.jar:19.22.0.0.0] at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:624) ~[ojdbc10-19.22.0.0.jar:19.22.0.0.0] at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:683) ~[java.sql:?] at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:191) ~[java.sql:?] <log cuntinues but have been cut down from here....>