synchronized Connection getConnection() throws JMSException, TemplateClosedException {
        if (closed.get()) {
            throw new JMSException("JMSTemplate Closed");
        }
        if (connection == null) {
            connection = getConnectionFactory().createConnection();
            connection.setExceptionListener(new ExceptionListener() {
                final Connection thisConn = connection;

                public void onException(JMSException exception) {
                    try {

                        boolean reset = false;
                        synchronized (this) {
                            if (connection == this.thisConn) {
                                reset = true;
                            }
                        }
                        if (reset) {
                            if (!closed.get() && reset) {
                                reset();
                            }
                        }
                    } catch (Throwable thrown) {
                        thrown.printStackTrace();
                    }
                }
            });
            connection.start();
        }
        return connection;
    }