Source Code Cross Referenced for Connection_base.java in  » Database-JDBC-Connection-Pool » octopus » com » internetcds » jdbc » tds » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database JDBC Connection Pool » octopus » com.internetcds.jdbc.tds 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //
002:        // Copyright 1998, 1999 CDS Networks, Inc., Medford Oregon
003:        //
004:        // All rights reserved.
005:        //
006:        // Redistribution and use in source and binary forms, with or without
007:        // modification, are permitted provided that the following conditions are met:
008:        // 1. Redistributions of source code must retain the above copyright
009:        //    notice, this list of conditions and the following disclaimer.
010:        // 2. Redistributions in binary form must reproduce the above copyright
011:        //    notice, this list of conditions and the following disclaimer in the
012:        //    documentation and/or other materials provided with the distribution.
013:        // 3. All advertising materials mentioning features or use of this software
014:        //    must display the following acknowledgement:
015:        //      This product includes software developed by CDS Networks, Inc.
016:        // 4. The name of CDS Networks, Inc.  may not be used to endorse or promote
017:        //    products derived from this software without specific prior
018:        //    written permission.
019:        //
020:        // THIS SOFTWARE IS PROVIDED BY CDS NETWORKS, INC. ``AS IS'' AND
021:        // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022:        // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023:        // ARE DISCLAIMED.  IN NO EVENT SHALL CDS NETWORKS, INC. BE LIABLE
024:        // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
025:        // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
026:        // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
027:        // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
028:        // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
029:        // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
030:        // SUCH DAMAGE.
031:        //
032:
033:        package com.internetcds.jdbc.tds;
034:
035:        import java.sql.*;
036:        import java.util.Properties;
037:        import java.util.Vector;
038:
039:        class TdsInstance {
040:            public static final String cvsVersion = "$Id: Connection_base.java,v 1.2 2007-10-19 13:21:40 sinisa Exp $";
041:
042:            public boolean inUse = false;
043:            public Tds tds = null;
044:
045:            public TdsInstance(Tds tds_) {
046:                tds = tds_;
047:                inUse = false;
048:            }
049:        }
050:
051:        /**
052:         * <P>A Connection represents a session with a specific
053:         * database. Within the context of a Connection, SQL statements are
054:         * executed and results are returned.
055:         *
056:         * <P>A Connection's database is able to provide information
057:         * describing its tables, its supported SQL grammar, its stored
058:         * procedures, the capabilities of this connection, etc. This
059:         * information is obtained with the getMetaData method.
060:         *
061:         * <P><B>Note:</B> By default the Connection automatically commits
062:         * changes after executing each statement. If auto commit has been
063:         * disabled, an explicit commit must be done or database changes will
064:         * not be saved.
065:         *
066:         * @author Craig Spannring
067:         * @author Igor Petrovski
068:         * @author The FreeTDS project
069:         * @version  $Id: Connection_base.java,v 1.2 2007-10-19 13:21:40 sinisa Exp $
070:         *
071:         * @see DriverManager#getConnection
072:         * @see Statement
073:         * @see ResultSet
074:         * @see DatabaseMetaData
075:         */
076:        public class Connection_base implements  ConnectionHelper {
077:            public static final String cvsVersion = "$Id: Connection_base.java,v 1.2 2007-10-19 13:21:40 sinisa Exp $";
078:
079:            String host = null;
080:            int serverType = -1; // Can be either Driver.SYBASE or Driver.SQLSERVER
081:            int port = -1; // Port numbers are _unsigned_ 16 bit, short is too small
082:            String database = null;
083:            Properties initialProps = null;
084:
085:            Vector tdsPool = null;
086:            DatabaseMetaData databaseMetaData = null;
087:            Vector allStatements = null;
088:            //   Tds 					tdsAll			  = null;
089:
090:            boolean autoCommit = true;
091:            int transactionIsolationLevel = java.sql.Connection.TRANSACTION_READ_COMMITTED;
092:            boolean isClosed = false;
093:
094:            private SQLWarningChain warningChain;
095:
096:            protected void NotImplemented() throws java.sql.SQLException {
097:                throw new java.sql.SQLException("Not Implemented");
098:            }
099:
100:            /**
101:             * Connect via TDS to a database server.
102:             *
103:             * @return a valid connection profile
104:             * @exception SQLException if a database access error occurs
105:             */
106:            public Connection_base(Properties props_) throws SQLException,
107:                    com.internetcds.jdbc.tds.TdsException {
108:                host = props_.getProperty("HOST");
109:                serverType = Integer.parseInt(props_.getProperty("SERVERTYPE"));
110:                port = Integer.parseInt(props_.getProperty("PORT"));
111:                database = props_.getProperty("DBNAME");
112:                String user = props_.getProperty("user");
113:                String password = props_.getProperty("password");
114:                initialProps = props_;
115:
116:                warningChain = new SQLWarningChain();
117:
118:                if (user == null) {
119:                    user = props_.getProperty("USER");
120:                    if (user == null) {
121:                        throw new SQLException("Need a username.");
122:                    }
123:                    props_.put("user", user);
124:                }
125:
126:                if (password == null) {
127:                    password = props_.getProperty("PASSWORD");
128:                    if (password == null) {
129:                        throw new SQLException("Need a password.");
130:                    }
131:                    props_.put("password", password);
132:                }
133:
134:                if (tdsPool == null) {
135:                    tdsPool = new Vector(20);
136:                }
137:
138:                if (allStatements == null) {
139:                    allStatements = new Vector(2);
140:                }
141:
142:                try {
143:                    //sinisa
144:                    Tds tmpTds = this .allocateTds();
145:                    //		  tdsAll = this.allocateTds();
146:                    freeTds(tmpTds);
147:                    //        freeTds(tdsAll);
148:                } catch (java.net.UnknownHostException e) {
149:                    throw new SQLException("Unknown host");
150:                } catch (java.io.IOException e) {
151:                    throw new SQLException("Network error- " + e.getMessage());
152:                }
153:            }
154:
155:            protected String sqlStatementToInitialize() {
156:                return serverType == Tds.SYBASE ? "set quoted_identifier on set textsize 50000"
157:                        : "";
158:            }
159:
160:            protected String sqlStatementToSetTransactionIsolationLevel()
161:                    throws SQLException {
162:                String sql = "set transaction isolation level ";
163:
164:                if (serverType == Tds.SYBASE) {
165:                    switch (transactionIsolationLevel) {
166:                    case java.sql.Connection.TRANSACTION_READ_UNCOMMITTED: {
167:                        throw new SQLException("Bad transaction level");
168:                    }
169:                    case java.sql.Connection.TRANSACTION_READ_COMMITTED: {
170:                        sql = sql + "1";
171:                        break;
172:                    }
173:                    case java.sql.Connection.TRANSACTION_REPEATABLE_READ: {
174:                        throw new SQLException("Bad transaction level");
175:                    }
176:                    case java.sql.Connection.TRANSACTION_SERIALIZABLE: {
177:                        sql = sql + "3";
178:                        break;
179:                    }
180:                    case java.sql.Connection.TRANSACTION_NONE:
181:                    default: {
182:                        throw new SQLException("Bad transaction level");
183:                    }
184:                    }
185:                } else {
186:                    switch (transactionIsolationLevel) {
187:                    case java.sql.Connection.TRANSACTION_READ_UNCOMMITTED: {
188:                        sql = sql + " read uncommitted ";
189:                        break;
190:                    }
191:                    case java.sql.Connection.TRANSACTION_READ_COMMITTED: {
192:                        sql = sql + " read committed ";
193:                        break;
194:                    }
195:                    case java.sql.Connection.TRANSACTION_REPEATABLE_READ: {
196:                        sql = sql + " repeatable read ";
197:                        break;
198:                    }
199:                    case java.sql.Connection.TRANSACTION_SERIALIZABLE: {
200:                        throw new SQLException("SQLServer does not support "
201:                                + "TRANSACTION_SERIALIZABLE");
202:                    }
203:                    case java.sql.Connection.TRANSACTION_NONE:
204:                    default: {
205:                        throw new SQLException("Bad transaction level");
206:                    }
207:                    }
208:                }
209:                return sql;
210:            }
211:
212:            protected String sqlStatementToSetCommit() {
213:                String result;
214:
215:                if (serverType == Tds.SYBASE) {
216:                    if (autoCommit) {
217:                        result = "set CHAINED off ";
218:                    } else {
219:                        result = "set CHAINED on ";
220:                    }
221:                } else {
222:                    if (autoCommit) {
223:                        result = "set implicit_transactions off ";
224:                    } else {
225:                        result = "set implicit_transactions on ";
226:                    }
227:                }
228:                return result;
229:            }
230:
231:            protected String sqlStatementForSettings() throws SQLException {
232:                //zoran
233:                return sqlStatementToInitialize()
234:                        + " "
235:                        + "set quoted_identifier,ansi_null_dflt_on,ansi_padding,ansi_warnings,ansi_nulls on set textsize 2147483647 "
236:                        + sqlStatementToSetTransactionIsolationLevel() + " ";
237:                //         sqlStatementToSetCommit();
238:            }
239:
240:            public String getUrl() {
241:                // XXX Is it legal to return something that might not be
242:                // exactly the URL used to connect?
243:                return ("jdbc:freetds:"
244:                        + (serverType == Tds.SYBASE ? "sybase" : "sqlserver")
245:                        + "://" + host + ":" + port + "/" + database);
246:            }
247:
248:            /**
249:             * allocate a tds instance to the calling thread.
250:             * <br>
251:             * The routine tries to reuse an available tds instance.  If there
252:             * are no tds instances that aren't in use it will create a new
253:             * instance.
254:             *
255:             * @exception java.sql.SQLException
256:             * @exception java.net.UnknownHostException
257:             * @exception com.internetcds.jdbc.tds.TdsException
258:             * @exception java.io.IOException
259:             *
260:             * @return A tds instance to use for database communications.
261:             */
262:            synchronized private Tds allocateTds()
263:                    throws java.sql.SQLException,
264:                    java.net.UnknownHostException,
265:                    com.internetcds.jdbc.tds.TdsException, java.io.IOException {
266:                Tds result;
267:                int i;
268:
269:                i = findAnAvailableTds();
270:                if (i == -1) {
271:                    Tds tmpTds = null;
272:                    try {
273:                        tmpTds = new Tds((java.sql.Connection) this ,
274:                                initialProps, sqlStatementForSettings());
275:                    } catch (SQLException e) {
276:                        throw new SQLException(e.getMessage() + "\n"
277:                                + tdsPool.size()
278:                                + " connection are in use by this program");
279:                    }
280:                    TdsInstance tmp = new TdsInstance(tmpTds);
281:                    tdsPool.addElement(tmp);
282:                    i = findAnAvailableTds();
283:                }
284:                if (i == -1) {
285:                    throw new TdsException(
286:                            "Internal Error.  Couldn't get tds instance");
287:                }
288:                if (((TdsInstance) tdsPool.elementAt(i)).inUse) {
289:                    throw new TdsException("Internal Error.  tds " + i
290:                            + " is already allocated");
291:                }
292:                ((TdsInstance) tdsPool.elementAt(i)).inUse = true;
293:                result = ((TdsInstance) (tdsPool.elementAt(i))).tds;
294:
295:                result.changeSettings(null, sqlStatementForSettings());
296:
297:                return result;
298:            }
299:
300:            /**
301:             *  Find a tds in the TdsPool that is not in use.
302:             *
303:             * @return -1 if none were found, otherwise return the index a tds
304:             */
305:            private int findAnAvailableTds() {
306:                int i;
307:
308:                for (i = tdsPool.size() - 1; i >= 0
309:                        && ((TdsInstance) tdsPool.elementAt(i)).inUse; i--) {
310:                    // nop
311:                }
312:                return i;
313:            }
314:
315:            public void markAsClosed(java.sql.Statement stmt)
316:                    throws TdsException {
317:                if (!allStatements
318:                        .removeElement((com.internetcds.jdbc.tds.Statement) stmt)) {
319:                    //commented, if call close() more than once from application.      	
320:                    //throw new TdsException("Statement was not known by the connection");
321:                }
322:            }
323:
324:            /**
325:             * return a tds instance back to the tds pool for reuse.
326:             *
327:             * @see allocateTds
328:             */
329:            private void freeTds(Tds tds) throws TdsException {
330:                int i;
331:
332:                i = -1;
333:                do {
334:                    i++;
335:                } while (i < tdsPool.size()
336:                        && tds != ((TdsInstance) tdsPool.elementAt(i)).tds);
337:
338:                if (i < tdsPool.size()) {
339:                    ((TdsInstance) tdsPool.elementAt(i)).inUse = false;
340:
341:                    // XXX Should also send a cancel to the server and throw out any
342:                    // data that has already been sent.
343:                } else {
344:                    throw new TdsException(
345:                            "Tried to free a tds that wasn't in use");
346:                }
347:            }
348:
349:            /**
350:             * return a tds instance back to the tds pool for reuse.
351:             *
352:             * A thread that is using a tds instance should return the
353:             * instance back to the tds pool when it is finished using it.
354:             *
355:             * @see allocateTds
356:             */
357:            public void relinquish(Tds tds) throws TdsException {
358:                freeTds(tds);
359:            }
360:
361:            /**
362:             * SQL statements without parameters are normally executed using
363:             * Statement objects.  If the same SQL statement is executed many
364:             * times, it is more efficient to use a PreparedStatement
365:             *
366:             * JDBC 2.0
367:             *
368:             * Result sets created using the returned Statement will have
369:             * forward-only type, and read-only concurrency, by default.
370:             *
371:             * @return a new Statement object
372:             * @exception SQLException passed through from the constructor
373:             */
374:            public java.sql.Statement createStatement() throws SQLException {
375:                //sinisa
376:                Tds tmpTds = null;
377:                try {
378:                    tmpTds = this .allocateTds();
379:                } catch (com.internetcds.jdbc.tds.TdsException e) {
380:                    throw new SQLException(e.getMessage());
381:                } catch (java.io.IOException e) {
382:                    throw new SQLException(e.getMessage());
383:                }
384:                //sinisa
385:                com.internetcds.jdbc.tds.Statement result;
386:                result = new com.internetcds.jdbc.tds.Statement(this , tmpTds);
387:                //      result = new com.internetcds.jdbc.tds.Statement(this, tdsAll);
388:
389:                allStatements.addElement(result);
390:                return result;
391:            }
392:
393:            /**
394:             * A SQL statement with or without IN parameters can be
395:             * pre-compiled and stored in a PreparedStatement object. This
396:             * object can then be used to efficiently execute this statement
397:             * multiple times.
398:             *
399:             * <P><B>Note:</B> This method is optimized for handling
400:             * parametric SQL statements that benefit from precompilation. If
401:             * the driver supports precompilation, prepareStatement will send
402:             * the statement to the database for precompilation. Some drivers
403:             * may not support precompilation. In this case, the statement may
404:             * not be sent to the database until the PreparedStatement is
405:             * executed.  This has no direct affect on users; however, it does
406:             * affect which method throws certain SQLExceptions.
407:             *
408:             * JDBC 2.0
409:             *
410:             * Result sets created using the returned PreparedStatement will have
411:             * forward-only type and read-only concurrency, by default.
412:             *
413:             * @param sql a SQL statement that may contain one or more '?' IN
414:             * parameter placeholders
415:             * @return a new PreparedStatement object containing the
416:             * pre-compiled statement
417:             * @exception SQLException if a database-access error occurs.
418:             */
419:            public java.sql.PreparedStatement prepareStatement(String sql)
420:                    throws SQLException {
421:                java.sql.PreparedStatement result;
422:
423:                //sinisa
424:                Tds tmpTds = null;
425:                try {
426:                    tmpTds = this .allocateTds();
427:                } catch (java.io.IOException e) {
428:                    throw new SQLException(e.getMessage());
429:                } catch (com.internetcds.jdbc.tds.TdsException e) {
430:                    throw new SQLException(e.getMessage());
431:                }
432:
433:                result = Constructors.newPreparedStatement(this , tmpTds, sql);
434:                //      result = Constructors.newPreparedStatement(this, tdsAll, sql);
435:
436:                allStatements.addElement(result);
437:
438:                return result;
439:            }
440:
441:            /**
442:             * A SQL stored procedure call statement is handled by creating a
443:             * CallableStatement for it.  The CallableStatement provides methods
444:             * for setting up its IN and OUT parameters and methods for executing
445:             * it.
446:             *
447:             * <B>Note:</B> This method is optimised for handling stored procedure
448:             * call statements.  Some drivers may send the call statement to the
449:             * database when the prepareCall is done; others may wait until the
450:             * CallableStatement is executed.  This has no direct effect on users;
451:             * however, it does affect which method throws certain SQLExceptions
452:             *
453:             * JDBC 2.0
454:             *
455:             * Result sets created using the returned CallableStatement will have
456:             * forward-only type and read-only concurrency, by default.
457:             *
458:             * @param sql a SQL statement that may contain one or more '?' parameter
459:             *    placeholders.  Typically this statement is a JDBC function call
460:             *    escape string.
461:             * @return a new CallableStatement object containing the pre-compiled
462:             *    SQL statement
463:             * @exception SQLException if a database access error occurs
464:             */
465:            public java.sql.CallableStatement prepareCall(String sql)
466:                    throws SQLException {
467:                java.sql.CallableStatement result;
468:
469:                //sinisa
470:                Tds tmpTds = null;
471:                try {
472:                    tmpTds = this .allocateTds();
473:                } catch (java.io.IOException e) {
474:                    throw new SQLException(e.getMessage());
475:                } catch (com.internetcds.jdbc.tds.TdsException e) {
476:                    throw new SQLException(e.getMessage());
477:                }
478:
479:                result = Constructors.newCallableStatement(this , tmpTds, sql);
480:                //      result = Constructors.newCallableStatement(this, tdsAll, sql);
481:                allStatements.addElement(result);
482:
483:                return result;
484:            }
485:
486:            /**
487:             * A driver may convert the JDBC sql grammar into its system's
488:             * native SQL grammar prior to sending it; nativeSQL returns the
489:             * native form of the statement that the driver would have sent.
490:             *
491:             * @param sql a SQL statement that may contain one or more '?'
492:             *    parameter placeholders
493:             * @return the native form of this statement
494:             * @exception SQLException if a database access error occurs
495:             */
496:
497:            public String nativeSQL(String sql) throws SQLException {
498:                return Tds.toNativeSql(sql, serverType);
499:            }
500:
501:            /**
502:             * If a connection is in auto-commit mode, then all its SQL
503:             * statements will be executed and committed as individual
504:             * transactions.  Otherwise, its SQL statements are grouped into
505:             * transactions that are terminated by either commit() or
506:             * rollback().  By default, new connections are in auto-commit
507:             * mode.
508:             *
509:             * The commit occurs when the statement completes or the next
510:             * execute occurs, whichever comes first. In the case of
511:             * statements returning a ResultSet, the statement completes when
512:             * the last row of the ResultSet has been retrieved or the
513:             * ResultSet has been closed. In advanced cases, a single
514:             * statement may return multiple results as well as output
515:             * parameter values. Here the commit occurs when all results and
516:             * output param values have been retrieved.
517:             *
518:             * @param value true enables auto-commit; false disables
519:             * auto-commit.
520:             * @exception SQLException if a database-access error occurs.
521:             */
522:            public void setAutoCommit(boolean value) throws SQLException {
523:                int i;
524:                String sql = null;
525:
526:                autoCommit = value;
527:                sql = sqlStatementToSetCommit();
528:                //sinisa
529:                for (i = 0; i < allStatements.size(); i++) {
530:                    Statement stmt = (Statement) allStatements.elementAt(i);
531:                    //         Statement  stmt = (com.internetcds.jdbc.tds.Statement)this.createStatement();
532:
533:                    {
534:                        // Note-  stmt.execute implicitly eats the END_TOKEN
535:                        // that will come back from the commit command
536:                        stmt.execute(sql);
537:                        stmt.execute("BEGIN TRAN");
538:                    }
539:                }
540:            }
541:
542:            /**
543:             * Get the current auto-commit state.
544:             *
545:             * @return Current state of auto-commit mode.
546:             * @exception SQLException if a database-access error occurs.
547:             * @see #setAutoCommit
548:             */
549:            public boolean getAutoCommit() throws SQLException {
550:                return autoCommit;
551:            }
552:
553:            /**
554:             * Commit makes all changes made since the previous
555:             * commit/rollback permanent and releases any database locks
556:             * currently held by the Connection. This method should only be
557:             * used when auto commit has been disabled.
558:             *
559:             * @exception SQLException if a database-access error occurs.
560:             * @see #setAutoCommit
561:             */
562:            public void commit() throws SQLException {
563:                commitOrRollback(true);
564:            }
565:
566:            /**
567:             * Rollback drops all changes made since the previous
568:             * commit/rollback and releases any database locks currently held
569:             * by the Connection. This method should only be used when auto
570:             * commit has been disabled.
571:             *
572:             * @exception SQLException if a database-access error occurs.
573:             * @see #setAutoCommit
574:             */
575:            public void rollback() throws SQLException {
576:                commitOrRollback(false);
577:            }
578:
579:            private void commitOrRollback(boolean commit) throws SQLException {
580:                int i;
581:                SQLException exception = null;
582:
583:                if (autoCommit) {
584:                    throw new SQLException("This method should only be "
585:                            + " used when auto commit has been disabled.");
586:                }
587:
588:                // XXX race condition here.  It is possible that a statement could
589:                // close while running this for loop.
590:                //sinisa
591:                for (i = 0; i < allStatements.size(); i++) {
592:                    Statement stmt = (Statement) allStatements.elementAt(i);
593:                    //String sql = "IF @@TRANCOUNT > 0 COMMIT TRAN";
594:                    //String sqlRollback = "IF @@TRANCOUNT > 0 ROLLBACK TRAN ";
595:                    //Statement  stmt = (com.internetcds.jdbc.tds.Statement)this.createStatement();
596:
597:                    try {
598:                        if (commit) {
599:                            stmt.commit();
600:                            //             stmt.executeQuery(sql);
601:                        } else {
602:                            stmt.rollback();
603:                            //               stmt.executeQuery(sqlRollback);
604:                        }
605:                    }
606:                    // XXX need to put all of these into the warning chain.
607:                    //
608:                    // Don't think so, the warnings would belong to Statement anyway -- SB
609:                    catch (java.sql.SQLException e) {
610:                        exception = e;
611:                    } catch (java.io.IOException e) {
612:                        exception = new SQLException(e.getMessage());
613:                    } catch (com.internetcds.jdbc.tds.TdsException e) {
614:                        exception = new SQLException(e.getMessage());
615:                    }
616:
617:                    /*         if (stmt instanceof CallableStatement)
618:                     {
619:                     ((PreparedStatementHelper)stmt).dropAllProcedures();
620:                     throw new SQLException("Not implemented");
621:                     }
622:                     else if (stmt instanceof PreparedStatement)
623:                     {
624:                     ((PreparedStatementHelper)stmt).dropAllProcedures();
625:                     }
626:                     */
627:                }
628:                if (exception != null) {
629:                    throw exception;
630:                }
631:            }
632:
633:            /**
634:             * In some cases, it is desirable to immediately release a
635:             * Connection's database and JDBC resources instead of waiting for
636:             * them to be automatically released; the close method provides this
637:             * immediate release.
638:             *
639:             * <P><B>Note:</B> A Connection is automatically closed when it is
640:             * garbage collected. Certain fatal errors also result in a closed
641:             * Connection.
642:             *
643:             * @exception SQLException if a database-access error occurs.
644:             */
645:            public void close() throws SQLException {
646:                int i;
647:
648:                for (i = 0; i < allStatements.size(); i++) {
649:                    Statement stmt = (Statement) allStatements.elementAt(i);
650:
651:                    {
652:                        if (!stmt.isClosed())
653:                            stmt.close();
654:                    }
655:                }
656:
657:                for (i = 0; i < tdsPool.size(); i++) {
658:                    ((TdsInstance) tdsPool.elementAt(i)).tds.close();
659:                }
660:
661:                clearWarnings();
662:                isClosed = true;
663:            }
664:
665:            /**
666:             * Tests to see if a Connection is closed.
667:             *
668:             * @return true if the connection is closed; false if it's still open
669:             * @exception SQLException if a database-access error occurs.
670:             */
671:            public boolean isClosed() throws SQLException {
672:                return isClosed;
673:            }
674:
675:            /**
676:             * A connection's database is able to provide information describing
677:             * its tables, its supported SQL grammar, its stored procedures, the
678:             * capabilities of this connection, etc.  This information is made
679:             * available through a DatabaseMetaData object.
680:             *
681:             * @return a DatabaseMetaData object for this connection
682:             * @exception SQLException if a database access error occurs
683:             */
684:            public java.sql.DatabaseMetaData getMetaData() throws SQLException {
685:                try {
686:                    if (databaseMetaData == null) {
687:                        // The DatabaseMetaData may need the tds connection
688:                        // at some later time.  Therefore we shouldn't relinquish the
689:                        // tds.
690:                        Tds tds = this .allocateTds();
691:                        databaseMetaData = new com.internetcds.jdbc.tds.DatabaseMetaData(
692:                                this , tds);
693:                    }
694:                    return databaseMetaData;
695:                } catch (java.io.IOException e) {
696:                    throw new SQLException(e.getMessage());
697:                } catch (com.internetcds.jdbc.tds.TdsException e) {
698:                    throw new SQLException(e.getMessage());
699:                }
700:                //       catch(java.net.UnknownHostException e)
701:                //       {
702:                //          throw new SQLException(e.getMessage());
703:                //       }
704:            }
705:
706:            /**
707:             * You can put a connection in read-only mode as a hint to enable
708:             * database optimizations
709:             *
710:             * <B>Note:</B> setReadOnly cannot be called while in the middle
711:             * of a transaction
712:             *
713:             * @param readOnly - true enables read-only mode; false disables it
714:             * @exception SQLException if a database access error occurs
715:             */
716:            public void setReadOnly(boolean readOnly) throws SQLException {
717:                throw new SQLException("Not implemented (setReadOnly)");
718:            }
719:
720:            /**
721:             * Tests to see if the connection is in read-only mode.
722:             *
723:             * @return true if connection is read-only
724:             * @exception SQLException if a database-access error occurs.
725:             */
726:            public boolean isReadOnly() throws SQLException {
727:                throw new SQLException("Not implemented (isReadOnly)");
728:            }
729:
730:            /**
731:             * A sub-space of this Connection's database may be selected by setting a
732:             * catalog name. If the driver does not support catalogs it will
733:             * silently ignore this request.
734:             *
735:             * @exception SQLException if a database-access error occurs.
736:             */
737:            public void setCatalog(String catalog) throws SQLException {
738:                throw new SQLException("Not implemented (setCatalog)");
739:            }
740:
741:            /**
742:             * Return the Connection's current catalog name.
743:             *
744:             * @return the current catalog name or null
745:             * @exception SQLException if a database-access error occurs.
746:             */
747:            public String getCatalog() throws SQLException {
748:                throw new SQLException("Not implemented (getCatalog)");
749:            }
750:
751:            /**
752:             * You can call this method to try to change the transaction
753:             * isolation level using one of the TRANSACTION_* values.
754:             *
755:             * <P><B>Note:</B> setTransactionIsolation cannot be called while
756:             * in the middle of a transaction.
757:             *
758:             * @param level one of the TRANSACTION_* isolation values with the
759:             * exception of TRANSACTION_NONE; some databases may not support
760:             * other values
761:             * @exception SQLException if a database-access error occurs.
762:             * @see DatabaseMetaData#supportsTransactionIsolationLevel
763:             */
764:            public void setTransactionIsolation(int level) throws SQLException {
765:                int i;
766:                String sql;
767:
768:                transactionIsolationLevel = level;
769:
770:                sql = sqlStatementToSetTransactionIsolationLevel();
771:
772:                for (i = 0; i < allStatements.size(); i++) {
773:                    Statement stmt = (Statement) allStatements.elementAt(i);
774:
775:                    {
776:                        // Note-  stmt.execute implicitly eats the END_TOKEN
777:                        // that will come back from the commit command
778:                        stmt.execute(sql);
779:                    }
780:                }
781:            }
782:
783:            /**
784:             * Get this Connection's current transaction isolation mode.
785:             *
786:             * @return the current TRANSACTION_* mode value
787:             * @exception SQLException if a database-access error occurs.
788:             */
789:            public int getTransactionIsolation() throws SQLException {
790:                return transactionIsolationLevel;
791:            }
792:
793:            /**
794:             * The first warning reported by calls on this Connection is
795:             * returned.
796:             *
797:             * <P><B>Note:</B> Subsequent warnings will be chained to this
798:             * SQLWarning.
799:             *
800:             * @return the first SQLWarning or null
801:             * @exception SQLException if a database-access error occurs.
802:             */
803:            public SQLWarning getWarnings() throws SQLException {
804:                return warningChain.getWarnings();
805:            }
806:
807:            /**
808:             * After this call, getWarnings returns null until a new warning
809:             * is reported for this connection.
810:             *
811:             * @exception SQLException if a database access error occurs
812:             */
813:            public void clearWarnings() throws SQLException {
814:                warningChain.clearWarnings();
815:            }
816:
817:            //--------------------------JDBC 2.0-----------------------------
818:
819:            /**
820:             * JDBC 2.0
821:             *
822:             * Creates a <code>Statement</code> object that will generate
823:             * <code>ResultSet</code> objects with the given type and concurrency.
824:             * This method is the same as the <code>createStatement</code> method
825:             * above, but it allows the default result set
826:             * type and result set concurrency type to be overridden.
827:             *
828:             * @param resultSetType a result set type; see ResultSet.TYPE_XXX
829:             * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
830:             * @return a new Statement object
831:             * @exception SQLException if a database access error occurs
832:             */
833:            public java.sql.Statement createStatement(int resultSetType,
834:                    int resultSetConcurrency) throws SQLException {
835:
836:                Tds tmpTds = null;
837:                try {
838:                    tmpTds = this .allocateTds();
839:                } catch (com.internetcds.jdbc.tds.TdsException e) {
840:                    throw new SQLException(e.getMessage());
841:                } catch (java.io.IOException e) {
842:                    throw new SQLException(e.getMessage());
843:                }
844:                com.internetcds.jdbc.tds.Statement result;
845:                result = new com.internetcds.jdbc.tds.Statement(this , tmpTds);
846:
847:                allStatements.addElement(result);
848:
849:                return result;
850:            }
851:
852:            /**
853:             * JDBC 2.0
854:             *
855:             * Creates a <code>PreparedStatement</code> object that will generate
856:             * <code>ResultSet</code> objects with the given type and concurrency.
857:             * This method is the same as the <code>prepareStatement</code> method
858:             * above, but it allows the default result set
859:             * type and result set concurrency type to be overridden.
860:             *
861:             * @param resultSetType a result set type; see ResultSet.TYPE_XXX
862:             * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
863:             * @return a new PreparedStatement object containing the
864:             * pre-compiled SQL statement
865:             * @exception SQLException if a database access error occurs
866:             */
867:            public java.sql.PreparedStatement prepareStatement(String sql,
868:                    int resultSetType, int resultSetConcurrency)
869:                    throws SQLException {
870:                NotImplemented();
871:                return null;
872:            }
873:
874:            /**
875:             * JDBC 2.0
876:             *
877:             * Creates a <code>CallableStatement</code> object that will generate
878:             * <code>ResultSet</code> objects with the given type and concurrency.
879:             * This method is the same as the <code>prepareCall</code> method
880:             * above, but it allows the default result set
881:             * type and result set concurrency type to be overridden.
882:             *
883:             * @param resultSetType a result set type; see ResultSet.TYPE_XXX
884:             * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
885:             * @return a new CallableStatement object containing the
886:             * pre-compiled SQL statement
887:             * @exception SQLException if a database access error occurs
888:             */
889:            public java.sql.CallableStatement prepareCall(String sql,
890:                    int resultSetType, int resultSetConcurrency)
891:                    throws SQLException {
892:                NotImplemented();
893:                return null;
894:            }
895:
896:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.