Changeset 157

Show
Ignore:
Timestamp:
11/18/07 17:11:49 (1 year ago)
Author:
chris
Message:

Get EmdrosDatabase? objects from a pool.

Throw helpful error messages in DatabaseExceptions?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • lex/trunk/src/com/qwirx/lex/Lex.java

    r147 r157  
    1212import java.util.Hashtable; 
    1313import java.util.Map; 
     14import java.util.Stack; 
    1415 
    1516import jemdros.EmdrosEnv; 
     
    6263        } 
    6364        }; 
     65     
     66    static class EmdrosDatabasePool 
     67    { 
     68        private Stack m_Pool = new Stack(); 
     69        private String m_User, m_Host;  
     70         
     71        public EmdrosDatabasePool(String user, String host) 
     72        { 
     73            m_User = user; 
     74            m_Host = host; 
     75        } 
     76         
     77        public synchronized EmdrosDatabase get() 
     78        throws DatabaseException 
     79        { 
     80            if (m_Pool.size() == 0) 
     81            { 
     82                return create(); 
     83            } 
     84             
     85            return (EmdrosDatabase)m_Pool.pop(); 
     86        } 
     87         
     88        public synchronized void put(EmdrosDatabase db) 
     89        { 
     90            if (m_Pool.size() > 0) 
     91            { 
     92                db.delete(); 
     93            } 
     94            else 
     95            { 
     96                m_Pool.push(db); 
     97            } 
     98        } 
     99         
     100        public EmdrosDatabase create() 
     101        throws DatabaseException 
     102        { 
     103            loadLibrary(); 
     104             
     105            EmdrosEnv env = new EmdrosEnv(eOutputKind.kOKConsole,  
     106                eCharsets.kCSISO_8859_1, "localhost", "emdf", "changeme",  
     107                "wihebrew"); 
     108 
     109            if (!env.connectionOk())  
     110            { 
     111                showDatabaseError(env); 
     112                throw new IllegalStateException("Not connected to database "+ 
     113                        "("+env.getDBError()+")"); 
     114            } 
     115 
     116            EmdrosDatabase emdrosDb = new EmdrosDatabase( 
     117                    env, m_User, m_Host, "wihebrew", getLogDatabaseHandle()); 
     118             
     119            emdrosDb.createObjectTypeIfMissing("note"); 
     120            emdrosDb.createFeatureIfMissing("note",  "text",             "string"); 
     121            emdrosDb.createFeatureIfMissing("clause","logical_struct_id","integer"); 
     122            emdrosDb.createFeatureIfMissing("phrase","argument_name",    "string"); 
     123            emdrosDb.createFeatureIfMissing("phrase","type_id",          "integer"); 
     124            emdrosDb.createFeatureIfMissing("phrase","macrorole_number", "integer default -1"); 
     125            emdrosDb.createFeatureIfMissing("clause","logical_structure","string"); 
     126            emdrosDb.createFeatureIfMissing("verse", "bart_gloss",       "string"); 
     127            emdrosDb.createFeatureIfMissing("word",  "wordnet_gloss",    "string"); 
     128            emdrosDb.createFeatureIfMissing("word",  "wordnet_synset",   "integer"); 
     129             
     130            return emdrosDb; 
     131        } 
     132    } 
     133     
     134    private static Map m_EmdrosPools = new Hashtable(); 
     135    private static Map m_Ledger = new Hashtable(); 
    64136 
    65137        public static final EmdrosDatabase getEmdrosDatabase(String user,  
     
    67139        throws DatabaseException  
    68140    { 
    69         loadLibrary(); 
    70          
    71         String key = Thread.currentThread().getId() + "@" + user + 
    72                 "@" + host;  
    73          
    74         Map tlsMap = (Map)s_EmdrosDatabaseMap.get(); 
    75          
    76         EmdrosDatabase db = (EmdrosDatabase)tlsMap.get(key); 
    77         if (db != null) 
    78         { 
    79                 db.setLogDatabaseHandle(getLogDatabaseHandle()); 
    80                 return db; 
    81         } 
    82          
    83                 EmdrosEnv env = new EmdrosEnv(eOutputKind.kOKConsole,  
    84                         eCharsets.kCSISO_8859_1, "localhost", "emdf", "changeme",  
    85                         "wihebrew"); 
    86  
    87                 if (!env.connectionOk())  
    88         { 
    89                         showDatabaseError(env); 
    90                         throw new IllegalStateException("Not connected to database "+ 
    91                     "("+env.getDBError()+")"); 
    92                 } 
    93  
    94                 EmdrosDatabase emdrosDb = new EmdrosDatabase( 
    95                                 env, user, host, "wihebrew", getLogDatabaseHandle()); 
    96                  
    97         emdrosDb.createObjectTypeIfMissing("note"); 
    98         emdrosDb.createFeatureIfMissing("note",  "text",             "string"); 
    99                 emdrosDb.createFeatureIfMissing("clause","logical_struct_id","integer"); 
    100                 emdrosDb.createFeatureIfMissing("phrase","argument_name",    "string"); 
    101                 emdrosDb.createFeatureIfMissing("phrase","type_id",          "integer"); 
    102         emdrosDb.createFeatureIfMissing("phrase","macrorole_number", "integer default -1"); 
    103                 emdrosDb.createFeatureIfMissing("clause","logical_structure","string"); 
    104                 emdrosDb.createFeatureIfMissing("verse", "bart_gloss",       "string"); 
    105         emdrosDb.createFeatureIfMissing("word",  "wordnet_gloss",    "string"); 
    106         emdrosDb.createFeatureIfMissing("word",  "wordnet_synset",   "integer"); 
    107                  
    108         tlsMap.put(key, emdrosDb); 
    109          
    110                 return emdrosDb; 
    111         } 
     141        String key = user + "@" + host; 
     142         
     143        EmdrosDatabasePool pool = (EmdrosDatabasePool)m_EmdrosPools.get(key); 
     144         
     145        if (pool == null) 
     146        { 
     147            pool = new EmdrosDatabasePool(user, host); 
     148            m_EmdrosPools.put(key, pool); 
     149        } 
     150         
     151        EmdrosDatabase db = pool.get(); 
     152        m_Ledger.put(db, pool); 
     153        return db; 
     154        } 
     155     
     156    public static final void putEmdrosDatabase(EmdrosDatabase db) 
     157    { 
     158        EmdrosDatabasePool pool = (EmdrosDatabasePool)m_Ledger.get(db); 
     159        pool.put(db); 
     160    } 
    112161         
    113162        private static final Connection getLogDatabaseHandle() 
     
    266315                        System.out.println("SQLState: "     + ex.getSQLState()); 
    267316                        System.out.println("VendorError: "  + ex.getErrorCode()); 
    268                         throw new DatabaseException(ex, "Connecting to "+dsn); 
     317                        throw new DatabaseException("Failed to connect to database: " +  
     318                            dsn, ex); 
    269319        } 
    270320                catch (IllegalStateException ex) 
    271321                { 
    272322                System.err.println(ex); 
    273                 throw new DatabaseException(ex, "Connecting to "+dsn); 
     323                throw new DatabaseException("Failed to connect to database: " +  
     324                    dsn, ex); 
    274325        } 
    275326