Search in sources :

Example 1 with ObjectCreationException

use of org.spf4j.recyclable.ObjectCreationException in project spf4j by zolyfarkas.

the class JdbcConnectionFactory method create.

@Override
public Connection create() throws ObjectCreationException {
    final Connection conn;
    try {
        conn = DriverManager.getConnection(url, user, password);
    } catch (SQLException ex) {
        throw new ObjectCreationException(ex);
    }
    return (Connection) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { Connection.class }, new InvocationHandler() {

        private Exception ex;

        @Override
        public Object invoke(final Object proxy, final Method method, final Object[] args) throws Exception {
            if ("close".equals(method.getName())) {
                pool.recycle((Connection) proxy, ex);
                ex = null;
                return null;
            } else {
                try {
                    return method.invoke(conn, args);
                } catch (IllegalAccessException | InvocationTargetException | RuntimeException e) {
                    ex = e;
                    throw e;
                }
            }
        }
    });
}
Also used : SQLException(java.sql.SQLException) ObjectCreationException(org.spf4j.recyclable.ObjectCreationException) Connection(java.sql.Connection) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) SQLException(java.sql.SQLException) ObjectCreationException(org.spf4j.recyclable.ObjectCreationException) ObjectDisposeException(org.spf4j.recyclable.ObjectDisposeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

InvocationHandler (java.lang.reflect.InvocationHandler)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ObjectCreationException (org.spf4j.recyclable.ObjectCreationException)1 ObjectDisposeException (org.spf4j.recyclable.ObjectDisposeException)1