001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.state.sqlite;
019
020import java.io.File;
021
022import net.sf.hajdbc.logging.Level;
023import net.sf.hajdbc.logging.Logger;
024import net.sf.hajdbc.logging.LoggerFactory;
025import net.sf.hajdbc.pool.AbstractPoolProvider;
026
027import org.tmatesoft.sqljet.core.SqlJetException;
028import org.tmatesoft.sqljet.core.table.SqlJetDb;
029
030/**
031 * <a href="http://sqljet.com/">SQLJet</a> is a java port of <a href="http://www.sqlite.org/">SQLite</a>.
032 * 
033 * @author paul
034 */
035public class SQLiteDbPoolProvider extends AbstractPoolProvider<SqlJetDb, SqlJetException>
036{
037        private Logger logger = LoggerFactory.getLogger(this.getClass());
038
039        private final File file;
040
041        public SQLiteDbPoolProvider(File file)
042        {
043                super(SqlJetDb.class, SqlJetException.class);
044                this.file = file;
045        }
046
047        /**
048         * {@inheritDoc}
049         * @see net.sf.hajdbc.pool.PoolProvider#close(java.lang.Object)
050         */
051        @Override
052        public void close(SqlJetDb database)
053        {
054                try
055                {
056                        database.close();
057                }
058                catch (SqlJetException e)
059                {
060                        this.logger.log(Level.WARN, e, e.getMessage());
061                }
062        }
063
064        /**
065         * {@inheritDoc}
066         * @see net.sf.hajdbc.pool.PoolProvider#create()
067         */
068        @Override
069        public synchronized SqlJetDb create() throws SqlJetException
070        {
071                boolean exists = this.file.exists();
072                SqlJetDb db = SqlJetDb.open(this.file, true);
073                if (!exists)
074                {
075                        db.getOptions().setAutovacuum(true);
076                        db.getOptions().setIncrementalVacuum(true);
077                }
078                return db;
079        }
080
081        /**
082         * {@inheritDoc}
083         * @see net.sf.hajdbc.pool.PoolProvider#isValid(java.lang.Object)
084         */
085        @Override
086        public boolean isValid(SqlJetDb database)
087        {
088                return database.isOpen();
089        }
090}