|
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 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | package org.bridgedb.rdb; |
|---|
| 18 | |
|---|
| 19 | import java.sql.Connection; |
|---|
| 20 | import java.sql.DriverManager; |
|---|
| 21 | import java.sql.ResultSet; |
|---|
| 22 | import java.sql.SQLException; |
|---|
| 23 | import java.sql.Statement; |
|---|
| 24 | |
|---|
| 25 | import org.bridgedb.IDMapperException; |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | public final class SimpleGdbFactory |
|---|
| 34 | { |
|---|
| 35 | |
|---|
| 36 | private SimpleGdbFactory() {}; |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | } |
|---|