net.sf.hajdbc.state.sqlite.SQLiteDbPoolProvider Maven / Gradle / Ivy
/*
* HA-JDBC: High-Availability JDBC
* Copyright (C) 2012 Paul Ferraro
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package net.sf.hajdbc.state.sqlite;
import java.io.File;
import net.sf.hajdbc.logging.Level;
import net.sf.hajdbc.logging.Logger;
import net.sf.hajdbc.logging.LoggerFactory;
import net.sf.hajdbc.pool.AbstractPoolProvider;
import org.tmatesoft.sqljet.core.SqlJetException;
import org.tmatesoft.sqljet.core.table.SqlJetDb;
/**
* SQLJet is a java port of SQLite.
*
* @author paul
*/
public class SQLiteDbPoolProvider extends AbstractPoolProvider
{
private Logger logger = LoggerFactory.getLogger(this.getClass());
private final File file;
public SQLiteDbPoolProvider(File file)
{
super(SqlJetDb.class, SqlJetException.class);
this.file = file;
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.pool.PoolProvider#close(java.lang.Object)
*/
@Override
public void close(SqlJetDb database)
{
try
{
database.close();
}
catch (SqlJetException e)
{
this.logger.log(Level.WARN, e, e.getMessage());
}
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.pool.PoolProvider#create()
*/
@Override
public synchronized SqlJetDb create() throws SqlJetException
{
boolean exists = this.file.exists();
SqlJetDb db = SqlJetDb.open(this.file, true);
if (!exists)
{
db.getOptions().setAutovacuum(true);
db.getOptions().setIncrementalVacuum(true);
}
return db;
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.pool.PoolProvider#isValid(java.lang.Object)
*/
@Override
public boolean isValid(SqlJetDb database)
{
return database.isOpen();
}
}