Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8755347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:50:19+00:00 2026-06-13T13:50:19+00:00

I have a Java application that uses lots of java.sql.Connection to a database. I

  • 0

I have a Java application that uses lots of java.sql.Connection to a database.

I want to test that, if the database is unavailable, my services return the appropriate error codes (distinguishing between temporary and permanent problems e.g. HTTP 500 and 503).

For testing, my application connects to an embedded, local, in-memory h2 database; the application is not aware of this, only my integration test is.

How can I make writes to the database fail deterministically, e.g. hook into commits and make them throw a custom SQLException? I want a global ‘database is unavailable’ boolean in the test code that affects all connections and makes my application exercise its reconnect logic.

(I had started by proxying Connection and putting an if(failFlag) throw new MySimulateFailureException() in commit(); but this didn’t catch PreparedStatement.executeUpdate(); before I embark on proxying the PreparedStatement too – its a lot of methods! – I’d like to be taught a better way…)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:50:20+00:00Added an answer on June 13, 2026 at 1:50 pm

    I ended up making my own Java reflection wrapper that intercepts Connection.commit and the PreparedStatement.execute... methods.

    My final code in my ‘DBFactory’ class:

    @SuppressWarnings("serial")
    public class MockFailureException extends SQLException {
        private MockFailureException() {
            super("The database has been deliberately faulted as part of a test-case");
        }
    }
    
    private class MockFailureWrapper implements InvocationHandler {
    
        final Object obj;
    
        private MockFailureWrapper(Object obj) {
            this.obj = obj;
        }
    
        @Override public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
            if(dbFailure && ("commit".equals(m.getName()) || m.getName().startsWith("execute")))
                throw new MockFailureException();
            Object result;
            try {
                result = m.invoke(obj, args);
                if(result instanceof PreparedStatement)
                    result = java.lang.reflect.Proxy.newProxyInstance(
                            result.getClass().getClassLoader(),
                            result.getClass().getInterfaces(),
                            new MockFailureWrapper(result));
            } catch (InvocationTargetException e) {
                throw e.getTargetException();
            } catch (Exception e) {
                throw new RuntimeException("unexpected invocation exception: " + e.getMessage());
            }
            return result;
        }
    
    }
    
    
    public Connection newConnection() throws SQLException {
        Connection connection = DriverManager.getConnection("jdbc:h2:mem:"+uuid+";CREATE=TRUE;DB_CLOSE_ON_EXIT=FALSE");
        return (Connection)java.lang.reflect.Proxy.newProxyInstance(
                connection.getClass().getClassLoader(),
                connection.getClass().getInterfaces(),
                new MockFailureWrapper(connection));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented an Java Swing application that uses an embedded JavaDB database. The
I have created a java SE application that uses the mysql database using mysql
I have a Java application that connects to a MySQL database and uses a
I have a java application that uses jtds driver and commons-dbcp as a connection
I have a Java application that uses an Apache Derby database with the embedded
I have a Java application (generic) that uses a database via hibernate. I ship
Our application is a Java-GWT application that uses Guice-Persist and Guice-Servlet extensively. We have
I have a standalone Java application that uses EclipseLink 2.0.1. It is configured by
I have Java ME Application that uses HttpConnection API, Here I am easily able
I have a Java web application that uses a plugin architecture. I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.