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.cache.eager; 019 020import java.sql.Connection; 021import java.sql.DatabaseMetaData; 022import java.sql.SQLException; 023 024import net.sf.hajdbc.Database; 025import net.sf.hajdbc.DatabaseCluster; 026import net.sf.hajdbc.DatabaseProperties; 027import net.sf.hajdbc.Messages; 028import net.sf.hajdbc.cache.DatabaseMetaDataCache; 029import net.sf.hajdbc.dialect.Dialect; 030 031/** 032 * DatabaseMetaDataCache implementation that eagerly caches data when first flushed. 033 * To be used when performance more of a concern than memory usage. 034 * 035 * @author Paul Ferraro 036 * @since 2.0 037 */ 038public class SharedEagerDatabaseMetaDataCache<Z, D extends Database<Z>> implements DatabaseMetaDataCache<Z, D> 039{ 040 private final DatabaseCluster<Z, D> cluster; 041 042 private volatile DatabaseProperties properties; 043 044 public SharedEagerDatabaseMetaDataCache(DatabaseCluster<Z, D> cluster) 045 { 046 this.cluster = cluster; 047 } 048 049 /** 050 * @see net.sf.hajdbc.cache.DatabaseMetaDataCache#flush() 051 */ 052 @Override 053 public void flush() throws SQLException 054 { 055 D database = this.cluster.getBalancer().next(); 056 057 if (database == null) 058 { 059 throw new SQLException(Messages.NO_ACTIVE_DATABASES.getMessage()); 060 } 061 062 this.setDatabaseProperties(database.connect(database.getConnectionSource(), database.decodePassword(this.cluster.getDecoder()))); 063 } 064 065 /** 066 * {@inheritDoc} 067 * @see net.sf.hajdbc.cache.DatabaseMetaDataCache#getDatabaseProperties(net.sf.hajdbc.Database, java.sql.Connection) 068 */ 069 @Override 070 public synchronized DatabaseProperties getDatabaseProperties(D database, Connection connection) throws SQLException 071 { 072 if (this.properties == null) 073 { 074 this.setDatabaseProperties(connection); 075 } 076 077 return this.properties; 078 } 079 080 private synchronized void setDatabaseProperties(Connection connection) throws SQLException 081 { 082 DatabaseMetaData metaData = connection.getMetaData(); 083 Dialect dialect = this.cluster.getDialect(); 084 this.properties = new EagerDatabaseProperties(metaData, dialect); 085 } 086}