Changeset 234

Show
Ignore:
Timestamp:
12/31/07 00:42:28 (1 year ago)
Author:
chris
Message:

Move Emdros database connection into EmdrosDatabase? class for better
encapsulation.

Check that database is alive before giving handle out from pool,
recreate if necessary.

Remove getVisibleMonadString(), use getVisibleMonads().toString() instead.

Use Emdros commands to get MIN_M and MAX_M rather than MQL queries,
more efficient.

Files:

Legend:

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

    r227 r234  
    2727import jemdros.TableIterator; 
    2828import jemdros.TableRow; 
     29import jemdros.eCharsets; 
     30import jemdros.eOutputKind; 
    2931 
    3032import org.apache.log4j.Logger; 
     
    4850    private static final Logger m_LOG = Logger.getLogger(EmdrosDatabase.class); 
    4951         
    50         public EmdrosDatabase(EmdrosEnv env, String username, String userhost, 
    51             String database, Connection conn)  
    52     { 
    53                 this.env = env; 
    54                 this.conn = conn; 
    55                 this.username = username; 
    56         this.userhost = userhost; 
    57                 this.database = database; 
    58         } 
    59          
    60         public void setLogDatabaseHandle(Connection conn) 
    61         { 
    62                 try 
    63                 { 
    64                         this.conn.close(); 
    65                 } 
    66                 catch (SQLException e) 
    67                 { 
    68                         m_LOG.error("Failed to close old database connection: "+e); 
    69                 } 
    70                 this.conn = conn;  
    71         } 
    72          
     52        public EmdrosDatabase(String dbHost, String dbName, String dbUser, 
     53        String dbPass, String logUser, String logFrom, Connection logDb)  
     54    throws DatabaseException 
     55    { 
     56        env = new EmdrosEnv(eOutputKind.kOKConsole,  
     57            eCharsets.kCSISO_8859_1, dbHost, dbUser, dbPass, dbName);  
     58 
     59        if (!env.connectionOk())  
     60        { 
     61            throw new DatabaseException("Failed to connect to database", 
     62                new Exception(env.getDBError())); 
     63        } 
     64 
     65        this.conn = logDb; 
     66        this.username = logUser; 
     67        this.userhost = logUser + "@" + logFrom; 
     68                this.database = dbName; 
     69        } 
     70 
     71    public boolean isAlive() 
     72    { 
     73        return env.connectionOk(); 
     74    } 
     75     
    7376        public void executeDirect(String query) throws DatabaseException  
    7477    { 
     78        m_LOG.info("Starting query: " + query); 
     79         
    7580        long startTime = System.currentTimeMillis(); 
    7681         
     
    96101                if (!bCompilerResult[0]) { 
    97102                        throw new DatabaseException( 
    98                                         "Compiler error: "+env.getCompilerError(), query); 
     103                                        "Compiler error",  
     104                    new Exception(env.getCompilerError()), query); 
    99105                } 
    100106         
     
    144150        public int getMinM() throws DatabaseException  
    145151    { 
    146                 Table min_m_table = getTable("SELECT MIN_M"); 
    147         try 
    148         { 
    149             return Integer.parseInt 
    150             ( 
    151                 min_m_table.iterator().next().iterator().next() 
    152                         ); 
    153         } 
    154         catch (TableException e) 
    155         { 
    156             throw new DatabaseException("Failed to get MIN_M", e,  
    157                 "SELECT MIN_M"); 
    158         } 
    159         } 
    160  
    161         public int getMaxM() throws DatabaseException  
    162     { 
    163                 Table max_m_table = getTable("SELECT MAX_M"); 
    164          
    165         try 
    166         { 
    167             return Integer.parseInt 
    168             ( 
    169                 max_m_table.iterator().next().iterator().next() 
    170                         ); 
    171         } 
    172         catch (TableException e) 
    173         { 
    174             throw new DatabaseException("Failed to get MAX_M", e,  
    175                 "SELECT MAX_M"); 
    176         } 
    177         } 
    178          
     152        int [] min_m = new int[1]; 
     153         
     154            if (!env.getMin_m(min_m)) 
     155        { 
     156                throw new DatabaseException("Failed to get MIN_M",  
     157                new Exception(env.getDBError()));    
     158        } 
     159         
     160        return min_m[0]; 
     161        } 
     162 
     163    public int getMaxM() throws DatabaseException  
     164    { 
     165        int [] max_m = new int[1]; 
     166         
     167        if (!env.getMax_m(max_m)) 
     168        { 
     169            throw new DatabaseException("Failed to get MAX_M",  
     170                new Exception(env.getDBError()));    
     171        } 
     172         
     173        return max_m[0]; 
     174    } 
     175 
    179176        public Map<String, String> getEnumerationConstants(String type, 
    180177        boolean byName)  
     
    431428    } 
    432429 
    433     public String getVisibleMonadString() 
    434     throws SQLException 
    435     { 
    436         PreparedStatement stmt = conn.prepareStatement 
    437         ( 
    438             "SELECT Monad_First, Monad_Last " + 
    439             "FROM   user_text_access " + 
    440             "WHERE  (User_Name = ? OR User_Name = 'anonymous')" 
    441         ); 
    442         stmt.setString(1, username); 
    443          
    444         ResultSet rs = stmt.executeQuery(); 
    445         StringBuffer result = new StringBuffer(); 
    446         result.append("{"); 
    447         boolean haveMonads = false; 
    448          
    449         while (rs.next())  
    450         { 
    451             haveMonads = true; 
    452             String first = rs.getString(1); 
    453             String last  = rs.getString(2); 
    454             result.append(first+"-"+last); 
    455             if (!rs.isLast()) 
    456             { 
    457                 result.append(","); 
    458             } 
    459         } 
    460          
    461         result.append("}"); 
    462          
    463         stmt.close(); 
    464         rs.close(); 
    465          
    466         if (!haveMonads) 
    467         { 
    468             return null; 
    469         } 
    470          
    471         return result.toString(); 
    472     } 
    473  
    474430    public boolean canWriteTo(MatchedObject object) 
    475431    throws DatabaseException 
  • lex/trunk/src/com/qwirx/lex/Lex.java

    r216 r234  
    7979            } 
    8080             
    81             return m_Pool.pop(); 
     81            EmdrosDatabase db = m_Pool.pop(); 
     82             
     83            if (!db.isAlive()) 
     84            { 
     85                return create(); 
     86            } 
     87             
     88            return db; 
    8289        } 
    8390         
    8491        public synchronized void put(EmdrosDatabase db) 
    8592        { 
    86             if (m_Pool.size() > 0
     93            if (m_Pool.size() > 0 || !db.isAlive()
    8794            { 
    8895                db.delete(); 
     
    98105        { 
    99106            loadLibrary(); 
    100              
    101             EmdrosEnv env = new EmdrosEnv(eOutputKind.kOKConsole,  
    102                 eCharsets.kCSISO_8859_1, "localhost", "emdf", "changeme",  
    103                 "wihebrew"); 
    104  
    105             if (!env.connectionOk())  
    106             { 
    107                 showDatabaseError(env); 
    108                 throw new IllegalStateException("Not connected to database "+ 
    109                         "("+env.getDBError()+")"); 
    110             } 
    111  
    112             EmdrosDatabase emdrosDb = new EmdrosDatabase( 
    113                     env, m_User, m_Host, "wihebrew", getLogDatabaseHandle()); 
     107 
     108            EmdrosDatabase emdrosDb = new EmdrosDatabase("localhost",  
     109                "wihebrew", "emdf", "changeme", m_User, m_Host, 
     110                getLogDatabaseHandle()); 
    114111             
    115112            emdrosDb.createObjectTypeIfMissing("note");