001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.store.jdbc;
018
019import java.io.IOException;
020
021import org.apache.activemq.broker.Locker;
022import org.apache.activemq.broker.SuppressReplyException;
023import org.apache.activemq.util.DefaultIOExceptionHandler;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027/**
028 * @org.apache.xbean.XBean
029 */
030/*
031 * @deprecated Use more general {@link org.apache.activemq.util.LeaseLockerIOExceptionHandler} instead
032 */
033@Deprecated
034public class JDBCIOExceptionHandler extends DefaultIOExceptionHandler {
035    private static final Logger LOG = LoggerFactory.getLogger(JDBCIOExceptionHandler.class);
036
037    public JDBCIOExceptionHandler() {
038        setIgnoreSQLExceptions(false);
039        setStopStartConnectors(true);
040    }
041
042    // fail only when we get an authoritative answer from the db w/o exceptions
043    @Override
044    protected boolean hasLockOwnership() throws IOException {
045        boolean hasLock = true;
046        if (broker.getPersistenceAdapter() instanceof JDBCPersistenceAdapter) {
047            JDBCPersistenceAdapter jdbcPersistenceAdapter = (JDBCPersistenceAdapter) broker.getPersistenceAdapter();
048            Locker locker = jdbcPersistenceAdapter.getLocker();
049            if (locker != null) {
050                try {
051                    if (!locker.keepAlive()) {
052                        hasLock = false;
053                    }
054                } catch (SuppressReplyException ignoreWhileHandlingInProgress) {
055                } catch (IOException ignored) {
056                }
057
058                if (!hasLock) {
059                    LOG.warn("Lock keepAlive failed, no longer lock owner with: {}", locker);
060                    throw new IOException("Lock keepAlive failed, no longer lock owner with: " + locker);
061                }
062            }
063        }
064        return hasLock;
065    }
066
067}