1 33 34 package com.internetcds.jdbc.tds; 35 36 import java.sql.*; 37 import java.util.Properties ; 38 import java.util.Vector ; 39 40 class TdsInstance 41 { 42 public static final String cvsVersion = "$Id: Connection_base.java,v 1.1 2006/06/23 10:39:04 sinisa Exp $"; 43 44 public boolean inUse = false; 45 public Tds tds = null; 46 47 public TdsInstance(Tds tds_) 48 { 49 tds = tds_; 50 inUse = false; 51 } 52 } 53 54 55 56 57 82 public class Connection_base implements ConnectionHelper 83 { 84 public static final String cvsVersion = "$Id: Connection_base.java,v 1.1 2006/06/23 10:39:04 sinisa Exp $"; 85 86 87 String host = null; 88 int serverType = -1; int port = -1; String database = null; 91 Properties initialProps = null; 92 93 94 Vector tdsPool = null; 95 DatabaseMetaData databaseMetaData = null; 96 Vector allStatements = null; 97 99 boolean autoCommit = true; 100 int transactionIsolationLevel = java.sql.Connection.TRANSACTION_READ_COMMITTED; 101 boolean isClosed = false; 102 103 private SQLWarningChain warningChain; 104 105 protected void NotImplemented() throws java.sql.SQLException 106 { 107 throw new java.sql.SQLException ("Not Implemented"); 108 } 109 110 111 112 113 114 120 public Connection_base( 121 Properties props_) 122 throws SQLException, com.internetcds.jdbc.tds.TdsException 123 { 124 host = props_.getProperty("HOST"); 125 serverType = Integer.parseInt(props_.getProperty("SERVERTYPE")); 126 port = Integer.parseInt(props_.getProperty("PORT")); 127 database = props_.getProperty("DBNAME"); 128 String user = props_.getProperty("user"); 129 String password = props_.getProperty("password"); 130 initialProps = props_; 131 132 warningChain = new SQLWarningChain(); 133 134 if (user == null) 135 { 136 user = props_.getProperty("USER"); 137 if (user == null) 138 { 139 throw new SQLException("Need a username."); 140 } 141 props_.put("user", user); 142 } 143 144 if (password == null) 145 { 146 password = props_.getProperty("PASSWORD"); 147 if (password == null) 148 { 149 throw new SQLException("Need a password."); 150 } 151 props_.put("password", password); 152 } 153 154 if (tdsPool == null) 155 { 156 tdsPool = new Vector (20); 157 } 158 159 if (allStatements == null) 160 { 161 allStatements = new Vector (2); 162 } 163 164 try 165 { 166 Tds tmpTds = this.allocateTds(); 168 freeTds(tmpTds); 170 } 172 catch (java.net.UnknownHostException e) 173 { 174 throw new SQLException("Unknown host"); 175 } 176 catch (java.io.IOException e) 177 { 178 throw new SQLException("Network error- " + e.getMessage()); 179 } 180 } 181 182 protected String sqlStatementToInitialize() 183 { 184 return serverType==Tds.SYBASE ? "set quoted_identifier on set textsize 50000" : ""; 185 } 186 187 protected String sqlStatementToSetTransactionIsolationLevel() 188 throws SQLException 189 { 190 String sql = "set transaction isolation level "; 191 192 if (serverType == Tds.SYBASE) 193 { 194 switch (transactionIsolationLevel) 195 { 196 case java.sql.Connection.TRANSACTION_READ_UNCOMMITTED: 197 { 198 throw new SQLException("Bad transaction level"); 199 } 200 case java.sql.Connection.TRANSACTION_READ_COMMITTED: 201 { 202 sql = sql + "1"; 203 break; 204 } 205 case java.sql.Connection.TRANSACTION_REPEATABLE_READ: 206 { 207 throw new SQLException("Bad transaction level"); 208 } 209 case java.sql.Connection.TRANSACTION_SERIALIZABLE: 210 { 211 sql = sql + "3"; 212 break; 213 } 214 case java.sql.Connection.TRANSACTION_NONE: 215 default: 216 { 217 throw new SQLException("Bad transaction level"); 218 } 219 } 220 } 221 else 222 { 223 switch (transactionIsolationLevel) 224 { 225 case java.sql.Connection.TRANSACTION_READ_UNCOMMITTED: 226 { 227 sql = sql + " read uncommitted "; 228 break; 229 } 230 case java.sql.Connection.TRANSACTION_READ_COMMITTED: 231 { 232 sql = sql + " read committed "; 233 break; 234 } 235 case java.sql.Connection.TRANSACTION_REPEATABLE_READ: 236 { 237 sql = sql + " repeatable read "; 238 break; 239 } 240 case java.sql.Connection.TRANSACTION_SERIALIZABLE: 241 { 242 throw new SQLException("SQLServer does not support " + 243 "TRANSACTION_SERIALIZABLE"); 244 } 245 case java.sql.Connection.TRANSACTION_NONE: 246 default: 247 { 248 throw new SQLException("Bad transaction level"); 249 } 250 } 251 } 252 return sql; 253 } 254 255 256 protected String sqlStatementToSetCommit() 257 { 258 String result; 259 260 if (serverType == Tds.SYBASE) 261 { 262 if (autoCommit) 263 { 264 result = "set CHAINED off "; 265 } 266 else 267 { 268 result = "set CHAINED on "; 269 } 270 } 271 else 272 { 273 if (autoCommit) 274 { 275 result = "set implicit_transactions off "; 276 } 277 else 278 { 279 result = "set implicit_transactions on "; 280 } 281 } 282 return result; 283 } 284 285 protected String sqlStatementForSettings() 286 throws SQLException 287 { 288 return 290 sqlStatementToInitialize() + " " + 291 "set quoted_identifier,ansi_null_dflt_on,ansi_padding,ansi_warnings,ansi_nulls on set textsize 2147483647 "+ 292 sqlStatementToSetTransactionIsolationLevel() + " "; 293 } 295 296 297 public String getUrl() 298 { 299 return 302 ("jdbc:freetds:" 303 + (serverType==Tds.SYBASE?"sybase":"sqlserver") 304 + "://" + host + ":" + port + "/" + database); 305 } 306 307 321 synchronized private Tds allocateTds() 322 throws java.sql.SQLException , java.net.UnknownHostException , 323 com.internetcds.jdbc.tds.TdsException, java.io.IOException 324 { 325 Tds result; 326 int i; 327 328 329 i = findAnAvailableTds(); 330 if (i == -1) 331 { 332 Tds tmpTds = null; 333 try 334 { 335 tmpTds = new Tds((java.sql.Connection )this, 336 initialProps, sqlStatementForSettings()); 337 } 338 catch (SQLException e) 339 { 340 throw new SQLException(e.getMessage() + "\n" + tdsPool.size() 341 + " connection are in use by this program"); 342 } 343 TdsInstance tmp = new TdsInstance(tmpTds); 344 tdsPool.addElement(tmp); 345 i = findAnAvailableTds(); 346 } 347 if (i == -1) 348 { 349 throw new TdsException("Internal Error. Couldn't get tds instance"); 350 } 351 if (((TdsInstance)tdsPool.elementAt(i)).inUse) 352 { 353 throw new TdsException("Internal Error. tds " + i 354 + " is already allocated"); 355 } 356 ((TdsInstance)tdsPool.elementAt(i)).inUse = true; 357 result = ((TdsInstance)(tdsPool.elementAt(i))).tds; 358 359 result.changeSettings(null, sqlStatementForSettings()); 360 361 return result; 362 } 363 364 369 private int findAnAvailableTds() 370 { 371 int i; 372 373 for(i=tdsPool.size()-1; 374 i>=0 && ((TdsInstance)tdsPool.elementAt(i)).inUse; 375 i--) 376 { 377 } 379 return i; 380 } 381 382 383 384 public void markAsClosed(java.sql.Statement stmt) throws TdsException 385 { 386 if (!allStatements.removeElement((com.internetcds.jdbc.tds.Statement)stmt)) 387 { 388 } 391 } 392 393 398 private void freeTds(Tds tds) 399 throws TdsException 400 { 401 int i; 402 403 i = -1; 404 do 405 { 406 i++; 407 } while(i<tdsPool.size() 408 && tds != ((TdsInstance)tdsPool.elementAt(i)).tds); 409 410 if (i<tdsPool.size()) 411 { 412 ((TdsInstance)tdsPool.elementAt(i)).inUse = false; 413 414 } 417 else 418 { 419 throw new TdsException("Tried to free a tds that wasn't in use"); 420 } 421 } 422 423 431 public void relinquish(Tds tds) 432 throws TdsException 433 { 434 freeTds(tds); 435 } 436 437 438 451 public java.sql.Statement createStatement() throws SQLException 452 { 453 Tds tmpTds = null; 455 try 456 { 457 tmpTds = this.allocateTds(); 458 } 459 catch(com.internetcds.jdbc.tds.TdsException e) 460 { 461 throw new SQLException(e.getMessage()); 462 } 463 catch(java.io.IOException e) 464 { 465 throw new SQLException(e.getMessage()); 466 } 467 com.internetcds.jdbc.tds.Statement result; 469 result = new com.internetcds.jdbc.tds.Statement(this, tmpTds); 470 472 allStatements.addElement(result); 473 return result; 474 } 475 476 477 478 504 public java.sql.PreparedStatement prepareStatement(String sql) 505 throws SQLException 506 { 507 java.sql.PreparedStatement result; 508 509 Tds tmpTds = null; 511 try 512 { 513 tmpTds = this.allocateTds(); 514 } 515 catch (java.io.IOException e) 516 { 517 throw new SQLException(e.getMessage()); 518 } 519 catch (com.internetcds.jdbc.tds.TdsException e) 520 { 521 throw new SQLException(e.getMessage()); 522 } 523 524 result = Constructors.newPreparedStatement(this, tmpTds, sql); 525 527 allStatements.addElement(result); 528 529 return result; 530 } 531 532 556 public java.sql.CallableStatement prepareCall(String sql) throws SQLException 557 { 558 java.sql.CallableStatement result; 559 560 Tds tmpTds = null; 562 try 563 { 564 tmpTds = this.allocateTds(); 565 } 566 catch (java.io.IOException e) 567 { 568 throw new SQLException(e.getMessage()); 569 } 570 catch (com.internetcds.jdbc.tds.TdsException e) 571 { 572 throw new SQLException(e.getMessage()); 573 } 574 575 result = Constructors.newCallableStatement(this, tmpTds, sql); 576 allStatements.addElement(result); 578 579 return result; 580 } 581 582 592 593 public String nativeSQL(String sql) throws SQLException 594 { 595 return Tds.toNativeSql(sql, serverType); 596 } 597 598 599 620 public void setAutoCommit(boolean value) throws SQLException 621 { 622 int i; 623 String sql = null; 624 625 autoCommit = value; 626 sql = sqlStatementToSetCommit(); 627 for(i=0; i<allStatements.size(); i++) 629 { 630 Statement stmt = (Statement)allStatements.elementAt(i); 631 633 { 634 stmt.execute(sql); 637 stmt.execute("BEGIN TRAN"); 638 } 639 } 640 } 641 642 643 650 public boolean getAutoCommit() throws SQLException 651 { 652 return autoCommit; 653 } 654 655 656 665 public void commit() throws SQLException 666 { 667 commitOrRollback(true); 668 } 669 670 671 680 public void rollback() throws SQLException 681 { 682 commitOrRollback(false); 683 } 684 685 private void commitOrRollback(boolean commit) 686 throws SQLException 687 { 688 int i; 689 SQLException exception = null; 690 691 if (autoCommit) 692 { 693 throw new SQLException("This method should only be " + 694 " used when auto commit has been disabled."); 695 } 696 697 698 for(i=0; i<allStatements.size(); i++) 702 { 703 Statement stmt = (Statement)allStatements.elementAt(i); 704 708 try 709 { 710 if (commit) 711 { 712 stmt.commit(); 713 } 715 else 716 { 717 stmt.rollback(); 718 } 720 } 721 catch (java.sql.SQLException e) 725 { 726 exception = e; 727 } 728 catch (java.io.IOException e) 729 { 730 exception = new SQLException(e.getMessage()); 731 } 732 catch (com.internetcds.jdbc.tds.TdsException e) 733 { 734 exception = new SQLException(e.getMessage()); 735 } 736 737 747 } 748 if (exception != null) 749 { 750 throw exception; 751 } 752 } 753 754 766 public void close() throws SQLException 767 { 768 int i; 769 770 771 for(i=0; i<allStatements.size(); i++) 772 { 773 Statement stmt = (Statement)allStatements.elementAt(i); 774 775 { 776 if( !stmt.isClosed() ) 777 stmt.close(); 778 } 779 } 780 781 for(i=0; i<tdsPool.size(); i++) 782 { 783 ((TdsInstance)tdsPool.elementAt(i)).tds.close(); 784 } 785 786 clearWarnings(); 787 isClosed = true; 788 } 789 790 796 public boolean isClosed() throws SQLException 797 { 798 return isClosed; 799 } 800 801 810 public java.sql.DatabaseMetaData getMetaData() throws SQLException 811 { 812 try 813 { 814 if (databaseMetaData == null) 815 { 816 Tds tds = this.allocateTds(); 820 databaseMetaData = new com.internetcds.jdbc.tds.DatabaseMetaData(this, tds); 821 } 822 return databaseMetaData; 823 } 824 catch(java.io.IOException e) 825 { 826 throw new SQLException(e.getMessage()); 827 } 828 catch(com.internetcds.jdbc.tds.TdsException e) 829 { 830 throw new SQLException(e.getMessage()); 831 } 832 } 837 838 848 public void setReadOnly (boolean readOnly) throws SQLException 849 { 850 throw new SQLException("Not implemented (setReadOnly)"); 851 } 852 853 859 public boolean isReadOnly() throws SQLException 860 { 861 throw new SQLException("Not implemented (isReadOnly)"); 862 } 863 864 865 866 873 public void setCatalog(String catalog) throws SQLException 874 { 875 throw new SQLException("Not implemented (setCatalog)"); 876 } 877 878 884 public String getCatalog() throws SQLException 885 { 886 throw new SQLException("Not implemented (getCatalog)"); 887 } 888 889 890 903 public void setTransactionIsolation(int level) 904 throws SQLException 905 { 906 int i; 907 String sql; 908 909 transactionIsolationLevel = level; 910 911 sql = sqlStatementToSetTransactionIsolationLevel(); 912 913 for(i=0; i<allStatements.size(); i++) 914 { 915 Statement stmt = (Statement)allStatements.elementAt(i); 916 917 { 918 stmt.execute(sql); 921 } 922 } 923 } 924 925 926 927 933 public int getTransactionIsolation() throws SQLException 934 { 935 return transactionIsolationLevel; 936 } 937 938 939 949 public SQLWarning getWarnings() throws SQLException 950 { 951 return warningChain.getWarnings(); 952 } 953 954 960 public void clearWarnings() throws SQLException 961 { 962 warningChain.clearWarnings(); 963 } 964 965 967 981 public java.sql.Statement createStatement( 982 int resultSetType, 983 int resultSetConcurrency) 984 throws SQLException 985 { 986 987 Tds tmpTds = null; 988 try 989 { 990 tmpTds = this.allocateTds(); 991 } 992 catch(com.internetcds.jdbc.tds.TdsException e) 993 { 994 throw new SQLException(e.getMessage()); 995 } 996 catch(java.io.IOException e) 997 { 998 throw new SQLException(e.getMessage()); 999 } 1000 com.internetcds.jdbc.tds.Statement result; 1001 result = new com.internetcds.jdbc.tds.Statement(this, tmpTds); 1002 1003 allStatements.addElement(result); 1004 1005 return result; 1006 } 1007 1008 1009 1024 public java.sql.PreparedStatement prepareStatement( 1025 String sql, 1026 int resultSetType, 1027 int resultSetConcurrency) 1028 throws SQLException 1029 { 1030 NotImplemented(); 1031 return null; 1032 } 1033 1034 1035 1050 public java.sql.CallableStatement prepareCall( 1051 String sql, 1052 int resultSetType, 1053 int resultSetConcurrency) throws SQLException 1054 { 1055 NotImplemented(); 1056 return null; 1057 } 1058 1059 1060} 1061 1062 1063 | Popular Tags |