root/trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbFactory.java @ 318

Revision 318, 2.7 KB (checked in by martijn, 6 months ago)

Made SimpleGdb? thread-safe. Instantiate with con-
nection string instead of connection object, to allow
future implementation of thread pooling

  • Property svn:eol-style set to native
Line 
1// BridgeDb,
2// An abstraction layer for identifer mapping services, both local and online.
3// Copyright 2006-2009 BridgeDb developers
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17package org.bridgedb.rdb;
18
19import java.sql.Connection;
20import java.sql.DriverManager;
21import java.sql.ResultSet;
22import java.sql.SQLException;
23import java.sql.Statement;
24
25import org.bridgedb.IDMapperException;
26
27/**
28 * Factory class that instantiates the correct SimpleGdb implementation
29 * for a new / existing gdb,
30 * based on the schema version.
31 * Use this class for creating / connecting to any SimpleGdb database.
32 */
33public final class SimpleGdbFactory
34{
35        /** private constructor prevents instantiation. */
36        private SimpleGdbFactory() {};
37       
38        /**
39         * Opens a connection to the Gene Database located in the given file.
40         * <p>
41         * Use this instead of constructor to create an instance of SimpleGdb that matches the schema version.
42         * @param connectionString a JDBC Connection string
43         * @return a new Gdb
44         * @throws IDMapperException on failure
45        */
46        public static SimpleGdb createInstance(String dbName, String connectionString) throws IDMapperException
47        {
48                if(connectionString == null) throw new NullPointerException(); 
49
50                int version = 0;
51                Connection con = null;
52                ResultSet r = null;
53                Statement stmt = null;
54                try 
55                {
56                        con = DriverManager.getConnection(connectionString);
57                        stmt = con.createStatement();
58                        r = stmt.executeQuery("SELECT schemaversion FROM info");
59                        if(r.next()) version = r.getInt(1);
60                }
61                catch (SQLException e)
62                {
63                        //Ignore, older db's don't even have schema version
64                }
65                finally
66                {
67                        if (r != null) try { r.close(); } catch (SQLException ignore) {}
68                        if (stmt != null) try { stmt.close(); } catch (SQLException ignore) {}
69                        if (con != null) try { con.close(); } catch (SQLException ignore) {}
70                }
71               
72                switch (version)
73                {
74                case 2:
75                        return new SimpleGdbImpl2(dbName, connectionString);
76                case 3:
77                        return new SimpleGdbImpl3(dbName, connectionString);
78                //NB add future schema versions here
79                default:
80                        throw new IDMapperException ("Unrecognized schema version '" + version + "', please make sure you have the latest " +
81                                        "version of this software and databases");
82                }               
83        }
84}
Note: See TracBrowser for help on using the browser.