01: /*
02: * HA-JDBC: High-Availability JDBC
03: * Copyright (c) 2004-2007 Paul Ferraro
04: *
05: * This library is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU Lesser General Public License as published by the
07: * Free Software Foundation; either version 2.1 of the License, or (at your
08: * option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful, but WITHOUT
11: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13: * for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public License
16: * along with this library; if not, write to the Free Software Foundation,
17: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: *
19: * Contact: ferraro@users.sourceforge.net
20: */
21: package net.sf.hajdbc.cache;
22:
23: import java.lang.ref.Reference;
24: import java.lang.ref.SoftReference;
25: import java.sql.Connection;
26: import java.sql.SQLException;
27:
28: import net.sf.hajdbc.DatabaseProperties;
29:
30: /**
31: * DatabaseMetaDataCache implementation that lazily caches data when requested.
32: * Used when a compromise between memory usage and performance is desired.
33: * Caches DatabaseProperties using a soft reference to prevent <code>OutOfMemoryError</code>s.
34: *
35: * @author Paul Ferraro
36: * @since 2.0
37: */
38: public class LazyDatabaseMetaDataCache extends
39: AbstractDatabaseMetaDataCache {
40: private Reference<DatabaseProperties> propertiesRef = new SoftReference<DatabaseProperties>(
41: null);
42:
43: /**
44: * @see net.sf.hajdbc.DatabaseMetaDataCache#flush(java.sql.Connection)
45: */
46: @Override
47: public synchronized void flush(Connection connection) {
48: this .propertiesRef.clear();
49: }
50:
51: /**
52: * @see net.sf.hajdbc.DatabaseMetaDataCache#getDatabaseProperties(java.sql.Connection)
53: */
54: @Override
55: public synchronized DatabaseProperties getDatabaseProperties(
56: Connection connection) throws SQLException {
57: LazyDatabaseProperties.setConnection(connection);
58:
59: DatabaseProperties properties = this .propertiesRef.get();
60:
61: if (properties == null) {
62: properties = new LazyDatabaseProperties(this .dialect);
63:
64: this .propertiesRef = new SoftReference<DatabaseProperties>(
65: properties);
66: }
67:
68: return properties;
69: }
70: }
|