~brian-thomason/+junk/ha-jdbc

« back to all changes in this revision

Viewing changes to src/net/sf/hajdbc/cache/EagerDatabaseMetaDataCache.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:34:21 UTC
  • Revision ID: brian.thomason@canonical.com-20111220173421-p9jg95iq91jgdihh
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * HA-JDBC: High-Availability JDBC
 
3
 * Copyright (c) 2004-2007 Paul Ferraro
 
4
 * 
 
5
 * This library is free software; you can redistribute it and/or modify it 
 
6
 * under the terms of the GNU Lesser General Public License as published by the 
 
7
 * Free Software Foundation; either version 2.1 of the License, or (at your 
 
8
 * option) any later version.
 
9
 * 
 
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.sql.Connection;
 
24
import java.sql.SQLException;
 
25
 
 
26
import net.sf.hajdbc.Balancer;
 
27
import net.sf.hajdbc.Database;
 
28
import net.sf.hajdbc.DatabaseProperties;
 
29
import net.sf.hajdbc.Dialect;
 
30
 
 
31
/**
 
32
 * DatabaseMetaDataCache implementation that eagerly caches data when first flushed.
 
33
 * To be used when performance more of a concern than memory usage.
 
34
 * 
 
35
 * @author Paul Ferraro
 
36
 * @since 2.0
 
37
 */
 
38
public class EagerDatabaseMetaDataCache<D> extends AbstractDatabaseMetaDataCache
 
39
{
 
40
        private volatile DatabaseProperties properties;
 
41
        private final Balancer<D> balancer;
 
42
        
 
43
        public EagerDatabaseMetaDataCache(Dialect dialect, Balancer<D> balancer)
 
44
        {
 
45
                super(dialect);
 
46
 
 
47
                this.balancer = balancer;
 
48
        }
 
49
        
 
50
        /**
 
51
         * @see net.sf.hajdbc.DatabaseMetaDataCache#flush()
 
52
         */
 
53
        @Override
 
54
        public void flush() throws SQLException
 
55
        {
 
56
                Database<D> database = this.balancer.next();
 
57
                
 
58
                this.setDatabaseProperties(database.connect(database.createConnectionFactory()));
 
59
        }
 
60
 
 
61
        /**
 
62
         * @see net.sf.hajdbc.DatabaseMetaDataCache#getDatabaseProperties(java.sql.Connection)
 
63
         */
 
64
        @Override
 
65
        public synchronized DatabaseProperties getDatabaseProperties(Connection connection) throws SQLException
 
66
        {
 
67
                if (this.properties == null)
 
68
                {
 
69
                        this.setDatabaseProperties(connection);
 
70
                }
 
71
                
 
72
                return this.properties;
 
73
        }
 
74
        
 
75
        private synchronized void setDatabaseProperties(Connection connection) throws SQLException
 
76
        {
 
77
                this.properties = new EagerDatabaseProperties(connection.getMetaData(), this.factory, this.dialect);
 
78
        }
 
79
}
 
 
b'\\ No newline at end of file'