1 21 22 package org.apache.derby.impl.tools.ij; 23 24 import org.apache.derby.iapi.tools.i18n.LocalizedOutput; 25 26 import java.sql.Connection ; 27 import java.sql.PreparedStatement ; 28 import java.sql.Statement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.util.Hashtable ; 32 33 44 class Session { 45 static final String DEFAULT_NAME="CONNECTION"; 46 47 boolean singleSession = true; 48 Connection conn = null; 49 String tag, name; 50 Hashtable prepStmts = new Hashtable (); 51 Hashtable cursorStmts = new Hashtable (); 52 Hashtable cursors = new Hashtable (); 53 Hashtable asyncStmts = new Hashtable (); 54 boolean isJCC= false; boolean isDNC = false; Session(Connection newConn, String newTag, String newName) { 57 conn = newConn; 58 tag = newTag; 59 name = newName; 60 61 try 62 { 63 isJCC = conn.getMetaData().getDriverName().startsWith("IBM DB2 JDBC Universal Driver"); 64 isDNC = conn.getMetaData().getDriverName().startsWith("Apache Derby Network Client"); 65 66 } 67 catch (SQLException se) 68 { 69 } 72 } 73 74 Connection getConnection() { 75 return conn; 77 } 78 79 boolean getIsJCC() 80 { 81 return isJCC; 82 } 83 84 boolean getIsDNC() 85 { 86 return isDNC; 87 } 88 89 String getName() { 90 return name; 91 } 92 93 Object addPreparedStatement(String name, PreparedStatement ps) { 94 return prepStmts.put(name,ps); 95 } 96 97 Object addCursorStatement(String name, Statement s) { 98 return cursorStmts.put(name, s); 99 } 100 101 Object addCursor(String name, ResultSet rs) { 102 return cursors.put(name, rs); 103 } 104 105 Object addAsyncStatement(String name, AsyncStatement s) { 106 return asyncStmts.put(name, s); 107 } 108 109 PreparedStatement getPreparedStatement(String name) { 110 return (PreparedStatement ) prepStmts.get(name); 111 } 112 113 Statement getCursorStatement(String name) { 114 return (Statement )cursorStmts.get(name); 115 } 116 117 ResultSet getCursor(String name) { 118 return (ResultSet )cursors.get(name); 119 } 120 121 AsyncStatement getAsyncStatement(String name) { 122 return (AsyncStatement)asyncStmts.get(name); 123 } 124 125 boolean removePreparedStatement(String name) { 126 return prepStmts.remove(name)!=null; 127 } 128 129 boolean removeCursorStatement(String name) { 130 return cursorStmts.remove(name)!=null; 131 } 132 133 boolean removeCursor(String name) { 134 return cursors.remove(name)!=null; 135 } 136 137 void doPrompt(boolean newStatement, LocalizedOutput out, boolean multiSessions) { 138 if (multiSessions && singleSession) { 140 singleSession = false; 141 142 if (tag == null) tag = "("+name+")"; 143 else tag = tag.substring(0,tag.length()-1)+":"+name+")"; 144 } 145 146 if (!multiSessions && !singleSession) { 148 singleSession = true; 149 150 if (tag == null) {} 151 else if (tag.length() == name.length()+2) tag = null; 152 else tag = tag.substring(0,tag.length()-2-name.length())+")"; 153 } 154 155 utilMain.doPrompt(newStatement, out, tag); 156 } 157 158 void close() throws SQLException { 159 160 if (!conn.isClosed()) 161 { 162 if (!conn.getAutoCommit() && name != null && ! name.startsWith("XA")) 163 conn.rollback(); 164 conn.close(); 165 } 166 167 prepStmts.clear(); cursorStmts.clear(); 169 cursors.clear(); 170 asyncStmts.clear(); 171 172 conn = null; 173 } 174 175 } 176 | Popular Tags |