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;
020import java.sql.Connection;
021import java.sql.SQLException;
022import java.sql.Statement;
023import javax.sql.DataSource;
024import org.apache.activemq.broker.AbstractLocker;
025import org.apache.activemq.store.PersistenceAdapter;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029public abstract class AbstractJDBCLocker extends AbstractLocker {
030    private static final Logger LOG = LoggerFactory.getLogger(AbstractJDBCLocker.class);
031    protected DataSource dataSource;
032    private Statements statements;
033    protected JDBCPersistenceAdapter jdbcAdapter;
034
035    protected boolean createTablesOnStartup;
036    protected int queryTimeout = -1;
037
038    public void configure(PersistenceAdapter adapter) throws IOException {
039        if (adapter instanceof JDBCPersistenceAdapter) {
040            this.jdbcAdapter = (JDBCPersistenceAdapter) adapter;
041            this.dataSource = ((JDBCPersistenceAdapter) adapter).getLockDataSource();
042            // we cannot get the statements (yet) as they may be configured later
043        }
044    }
045
046    protected Statements getStatements() {
047        if (statements == null && jdbcAdapter != null) {
048            statements = jdbcAdapter.getStatements();
049        }
050        return statements;
051    }
052
053    public void setDataSource(DataSource dataSource) {
054        this.dataSource = dataSource;
055    }
056
057    public void setStatements(Statements statements) {
058        this.statements = statements;
059    }
060
061    protected void setQueryTimeout(Statement statement) throws SQLException {
062        if (queryTimeout > 0) {
063            statement.setQueryTimeout(queryTimeout);
064        }
065    }
066
067    public int getQueryTimeout() {
068        return queryTimeout;
069    }
070
071    public void setQueryTimeout(int queryTimeout) {
072        this.queryTimeout = queryTimeout;
073    }
074
075    public void setCreateTablesOnStartup(boolean createTablesOnStartup) {
076        this.createTablesOnStartup = createTablesOnStartup;
077    }
078
079    protected Connection getConnection() throws SQLException {
080        return dataSource.getConnection();
081    }
082
083    protected void close(Connection connection) {
084        if (null != connection) {
085            try {
086                connection.close();
087            } catch (SQLException e1) {
088                LOG.debug("exception while closing connection: " + e1, e1);
089            }
090        }
091    }
092
093    protected void close(Statement statement) {
094        if (null != statement) {
095            try {
096                statement.close();
097            } catch (SQLException e1) {
098                LOG.debug("exception while closing statement: " + e1, e1);
099            }
100        }
101    }
102
103    @Override
104    public void preStart() {
105        if (createTablesOnStartup) {
106
107            String[] createStatements = getStatements().getCreateLockSchemaStatements();
108
109            Connection connection = null;
110            Statement statement = null;
111            try {
112                connection = getConnection();
113                statement = connection.createStatement();
114                setQueryTimeout(statement);
115
116                for (int i = 0; i < createStatements.length; i++) {
117                    LOG.debug("Executing SQL: " + createStatements[i]);
118                    try {
119                        statement.execute(createStatements[i]);
120                    } catch (SQLException e) {
121                        LOG.info("Could not create lock tables; they could already exist." + " Failure was: "
122                                + createStatements[i] + " Message: " + e.getMessage() + " SQLState: " + e.getSQLState()
123                                + " Vendor code: " + e.getErrorCode());
124                    }
125                }
126            } catch (SQLException e) {
127                LOG.warn("Could not create lock tables; Failure Message: " + e.getMessage() + " SQLState: " + e.getSQLState()
128                        + " Vendor code: " + e.getErrorCode(), e);
129            } finally {
130                close(statement);
131                close(connection);
132            }
133        }
134    }
135
136}