KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > XAPoolTestSuite


1
2 import junit.framework.Assert;
3
4 import java.sql.PreparedStatement JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 import java.sql.Connection JavaDoc;
12 import java.sql.ResultSet JavaDoc;
13
14 import org.enhydra.jdbc.standard.StandardXAConnectionHandle;
15
16 /**
17  * This object does not contains TestCase from Junit, but only
18  * tests from XAPool. All tests begin with test...
19  * Please see setUp and tearDown from XAPoolTestCase to take a
20  * look to the initialisation of the database (XA) and JOTM
21  */

22 public class XAPoolTestSuite extends XAPoolTestCase {
23     public XAPoolTestSuite(String JavaDoc strTestName) {
24     super(strTestName);
25     }
26
27     public void testSimpleStatementCommit() throws Exception JavaDoc {
28     int newValue = 54;
29     String JavaDoc completion = "commit";
30     System.out.println("SimpleStatementCommit: begin, getConnection, executeUpdate, VERIFY, closeAll, commit, getConnection, VERIFY, close");
31     try {
32             utx.begin();
33         
34             conn = spds.getConnection(login, password);
35             PreparedStatement JavaDoc pstmt = conn.prepareStatement(SQL_QUERY);
36             pstmt.setInt(1, newValue);
37             pstmt.executeUpdate();
38         Assert.assertTrue("executeUpdate with "+newValue+" does not work", getValue() == 54);
39             pstmt.close();
40             conn.close();
41
42         utx.commit();
43
44         } catch (Exception JavaDoc e) {
45             System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown");
46             System.err.println("Exception message :" + e.getMessage());
47             e.printStackTrace();
48             throw e;
49         }
50
51         conn = spds.getConnection(login, password);
52     Assert.assertTrue("after commit with "+newValue+" does not work", getValue() == 54);
53         conn.close();
54     }
55     
56     public void testSimpleStatementRollback() throws Exception JavaDoc {
57     int newValue = 12;
58     String JavaDoc completion = "rollback";
59     System.out.println("SimpleStatementRollback: begin, getConnection, executeUpdate, VERIFY, closeAll, rollback, getConnection, VERIFY, close");
60     try {
61             utx.begin();
62         
63             conn = spds.getConnection(login, password);
64             PreparedStatement JavaDoc pstmt = conn.prepareStatement(SQL_QUERY);
65             pstmt.setInt(1, newValue);
66             pstmt.executeUpdate();
67         Assert.assertTrue("executeUpdate with "+newValue+" does not work", getValue() == 12);
68             pstmt.close();
69             conn.close();
70
71         utx.rollback();
72
73         } catch (Exception JavaDoc e) {
74             System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown");
75             System.err.println("Exception message :" + e.getMessage());
76             e.printStackTrace();
77             throw e;
78         }
79
80         conn = spds.getConnection(login, password);
81     Assert.assertTrue("after rollback with "+newValue+" does not work", getValue() == 54);
82         conn.close();
83     }
84     
85     public void testSaveAutoCommitTrue() throws Exception JavaDoc {
86     int newValue = 12;
87     System.out.println("SaveAutoCommitTrue: getConnection, setAutoCommit(true), begin, executeUpdate, rollback, VERIFY, closeAll");
88     try {
89             conn = spds.getConnection(login, password);
90             boolean autocom = true;
91             conn.setAutoCommit(autocom);
92         
93             utx.begin();
94             PreparedStatement JavaDoc pstmt = conn.prepareStatement(SQL_QUERY);
95             pstmt.setInt(1, newValue);
96             pstmt.executeUpdate();
97
98             utx.rollback();
99         Assert.assertTrue("Saveautocommit with TRUE does not work", conn.getAutoCommit() == autocom);
100         pstmt.close();
101             conn.close();
102         
103         } catch (Exception JavaDoc e) {
104             System.out.println("Exception of type :" + e.getClass().getName() + " has been thrown");
105             System.out.println("Exception message :" + e.getMessage());
106             e.printStackTrace();
107             throw e;
108         }
109     }
110
111     public void testSaveAutoCommitFalse() throws Exception JavaDoc{
112     int newValue = 12;
113     System.out.println("SaveAutoCommitFalse: getConnection, setAutoCommit(false), begin, executeUpdate, rollback, VERIFY, closeAll");
114         try {
115             conn = spds.getConnection(login, password);
116             boolean autocom = false;
117             conn.setAutoCommit(autocom);
118
119             utx.begin();
120             PreparedStatement JavaDoc pstmt = conn.prepareStatement(SQL_QUERY);
121             pstmt.setInt(1, newValue);
122             pstmt.executeUpdate();
123             utx.rollback();
124
125         Assert.assertTrue("Saveautocommit with FALSE does not work", conn.getAutoCommit() == autocom);
126         pstmt.close();
127             conn.close();
128
129         } catch (Exception JavaDoc e) {
130             System.out.println("Exception of type :" + e.getClass().getName() + " has been thrown");
131             System.out.println("Exception message :" + e.getMessage());
132             e.printStackTrace();
133             throw e;
134         }
135     }
136
137
138     public void testMultipleConnectionCommit() throws Exception JavaDoc {
139     int newValue = 66;
140     System.out.println("MultipleConnectionCommit: begin, getConnection, executeUpdate, VERIFY, closeAll, getConnection, executeUpdate, VERIFY, closeAll, commit, getConnection, VERIFY, close");
141         try {
142             utx.begin();
143
144             conn = spds.getConnection(login, password);
145             PreparedStatement JavaDoc pstmt = conn.prepareStatement(SQL_QUERY);
146             pstmt.setInt(1, newValue);
147             pstmt.executeUpdate();
148         Assert.assertTrue("executeUpdate with "+(newValue)+" does not work", getValue() == 66);
149             pstmt.close();
150             conn.close();
151
152         conn = spds.getConnection(login, password);
153         PreparedStatement JavaDoc pstmt2 = conn.prepareStatement(SQL_QUERY);
154             pstmt2.setInt(1, newValue+2);
155             pstmt2.executeUpdate();
156         Assert.assertTrue("executeUpdate with "+(newValue+2)+" does not work", getValue() == 68);
157             pstmt2.close();
158             conn.close();
159
160         utx.commit();
161
162         } catch (Exception JavaDoc e) {
163             System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown");
164             System.err.println("Exception message :" + e.getMessage());
165             e.printStackTrace();
166             throw e;
167         }
168
169         conn = spds.getConnection(login, password);
170     Assert.assertTrue("after commit with "+(newValue+2)+" does not work", getValue() == 68);
171         conn.close();
172     }
173
174
175     public void testMultipleConnectionRollback() throws Exception JavaDoc {
176     int newValue = 900;
177     System.out.println("MultipleConnectionCommit: begin, getConnection, executeUpdate, VERIFY, closeAll, getConnection, executeUpdate, VERIFY, closeAll, rollback, getConnection, VERIFY, close");
178         try {
179             utx.begin();
180
181             conn = spds.getConnection(login, password);
182             PreparedStatement JavaDoc pstmt = conn.prepareStatement(SQL_QUERY);
183             pstmt.setInt(1, newValue);
184             pstmt.executeUpdate();
185         Assert.assertTrue("executeUpdate with "+(newValue)+" does not work", getValue() == 900);
186             pstmt.close();
187             conn.close();
188
189         conn = spds.getConnection(login, password);
190         PreparedStatement JavaDoc pstmt2 = conn.prepareStatement(SQL_QUERY);
191             pstmt2.setInt(1, newValue+2);
192             pstmt2.executeUpdate();
193         Assert.assertTrue("executeUpdate with "+(newValue+2)+" does not work", getValue() == 902);
194             pstmt2.close();
195             conn.close();
196
197         utx.rollback(); // ***** ROLLBACK ******
198

199         } catch (Exception JavaDoc e) {
200             System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown");
201             System.err.println("Exception message :" + e.getMessage());
202             e.printStackTrace();
203             throw e;
204         }
205
206         conn = spds.getConnection(login, password);
207     Assert.assertTrue("after rollback with "+(newValue+2)+" does not work", getValue() == 68);
208         conn.close();
209     }
210
211     public void testMultipleTransaction() throws Exception JavaDoc {
212     System.out.println("MultipleTransaction: getConnection, begin, executeUpdate, VERIFY, commit, VERIFY, begin, executeUpdate, VERIFY, rollback, VERIFY, close");
213     try {
214         conn = spds.getConnection(login, password);
215     } catch(Exception JavaDoc e) {
216         System.out.println("problem");
217         if (conn == null) {
218         System.out.println("the connection is null");
219         // do some work
220
}
221         else {
222         System.out.println("the connection is not null");
223         // do some work
224
}
225     }
226     int newValue = 44;
227         try {
228             utx.begin();
229             PreparedStatement JavaDoc pstmt0 = conn.prepareStatement(SQL_QUERY);
230             pstmt0.setInt(1, 13);
231             pstmt0.executeUpdate();
232         pstmt0.close();
233         Assert.assertTrue("executeUpdate with 13 does not work", getValue() == 13);
234             utx.commit();
235
236         Assert.assertTrue("after commit with 13 does not work", getValue() == 13);
237
238             utx.begin();
239             PreparedStatement JavaDoc pstmt = conn.prepareStatement(SQL_QUERY);
240             pstmt.setInt(1, newValue);
241             pstmt.executeUpdate();
242         pstmt.close();
243         Assert.assertTrue("executeUpdate with "+newValue+" does not work", getValue() == 44);
244         
245         utx.rollback();
246
247         Assert.assertTrue("after rollback with "+newValue+" does not work", getValue() == 13);
248
249         } catch (Exception JavaDoc e) {
250             System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown");
251             System.err.println("Exception message :" + e.getMessage());
252             e.printStackTrace();
253             throw e;
254         }
255
256         conn.close();
257
258     }
259     
260     public void testPotentialDeadLock() throws Exception JavaDoc {
261     System.out.println("PotentialDeadLock: begin, getConnection, executeUpdate, VERIFY, pclose, VERIFY, close, VERIFY, commit, VERIFY");
262     int newValue = 44;
263         try {
264             utx.begin();
265         conn = spds.getConnection(login, password);
266             PreparedStatement JavaDoc pstmt0 = conn.prepareStatement(SQL_QUERY);
267             pstmt0.setInt(1, 133);
268             pstmt0.executeUpdate();
269         Assert.assertTrue("executeUpdate with 133 does not work", getValue() == 133);
270         pstmt0.close();
271         Assert.assertTrue("after stmt.close: executeUpdate with 133 does not work", getValue() == 133);
272         conn.close();
273         Assert.assertTrue("after con.close: executeUpdate with 133 does not work (value is "+getValue()+") ", getValue() == 133);
274         utx.commit();
275         Assert.assertTrue("after commit with 133 does not work", getValue() == 133);
276         } catch (Exception JavaDoc e) {
277             System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown");
278             System.err.println("Exception message :" + e.getMessage());
279             e.printStackTrace();
280             throw e;
281         }
282     }
283
284     public void testMiroRequestConnectionCountInTransaction() throws Exception JavaDoc {
285     StandardXAConnectionHandle tempCon = null;
286     StringBuffer JavaDoc sbBuffer = new StringBuffer JavaDoc();
287     String JavaDoc strCon;
288     PreparedStatement JavaDoc insertStatement = null;;
289     Map JavaDoc mpReturnPatternMap = new HashMap JavaDoc();
290     Map JavaDoc mpConnections = new HashMap JavaDoc();
291     Map JavaDoc mpUsedConnections = new HashMap JavaDoc();
292     List JavaDoc lstConnections = new ArrayList JavaDoc();
293     int iTable1Index = 0;
294     int iInsertCount;
295     System.out.println("RequestConnectionCountInTransaction: Request multiple connections in one transactions over and over and see if they will be returned in the same order.");
296     try {
297         try {
298         utx.begin();
299         
300         for (int iIndex = 0; iIndex < 10; iIndex++) {
301             try {
302             sbBuffer.delete(0, sbBuffer.length());
303             for (int iCon = 0; iCon < 4; iCon++) {
304                 conn = spds.getConnection();
305                 Assert.assertTrue("XAPool no longer returns instance" +
306                           " of StandardXAConnectionHandle",
307                           conn instanceof StandardXAConnectionHandle);
308                 tempCon = (StandardXAConnectionHandle)conn;
309                 strCon = tempCon.con.toString();
310                 sbBuffer.append(strCon);
311                 sbBuffer.append("::");
312                 lstConnections.add(conn);
313                 mpConnections.put(strCon, strCon);
314                 try {
315                 insertStatement = conn.prepareStatement(SQL_QUERY);
316                 insertStatement.setInt(1, iTable1Index++);
317                 iInsertCount = insertStatement.executeUpdate();
318                 Assert.assertEquals("One record should have been inserted.",
319                             1, iInsertCount);
320                 }
321                 finally {
322                 insertStatement.close();
323                 insertStatement = null;
324                 }
325                 tempCon = (StandardXAConnectionHandle)conn;
326                 strCon = tempCon.con.toString();
327                 mpUsedConnections.put(strCon, strCon);
328             }
329             mpReturnPatternMap.put(sbBuffer.toString(), sbBuffer.toString());
330             }
331             finally {
332             for (int iCon = 0; iCon < 4; iCon++) {
333                 ((Connection JavaDoc)lstConnections.remove(0)).close();
334             }
335             }
336         }
337         /*
338           System.out.println("Total allocated connections = "+ mpConnections.size());
339           System.out.println("Total used connections = " + mpUsedConnections.size());
340           System.out.println("Connections return pattern count = " + mpReturnPatternMap.size());
341           for (Iterator itrElements = mpReturnPatternMap.values().iterator(); itrElements.hasNext();) {
342           System.out.println(itrElements.next().toString());
343           }
344         */

345         Assert.assertEquals("More than expected number of connections were used.", 4, mpUsedConnections.size());
346         Assert.assertEquals("More than expected number of connections were allocated.", 4, mpConnections.size());
347         utx.commit();
348         }
349         catch (Throwable JavaDoc throwable) {
350         utx.rollback();
351         }
352         
353     } finally {
354         /*
355           PreparedStatement deleteStatement = null;
356           
357           utx.begin();
358           try {
359           try {
360           deleteStatement = m_connection.prepareStatement(DELETE_ALL1);
361           int iDeleteCount = DatabaseUtils.executeUpdateAndClose(deleteStatement);
362           } finally {
363           DatabaseUtils.closeStatement(deleteStatement);
364           deleteStatement = null;
365           }
366           utx.commit();
367           } catch (Throwable throwable) {
368           utx.rollback();
369         }
370         */

371     }
372     }
373     
374
375
376
377
378     
379     public void testMiroRequestConnectionCountOutOfTransaction() throws Exception JavaDoc {
380     Connection JavaDoc con = null;
381     StandardXAConnectionHandle tempCon;
382     String JavaDoc strCon;
383     int iInsertCount;
384     PreparedStatement JavaDoc insertStatement = null;;
385     Map JavaDoc mpReturnPatternMap = new HashMap JavaDoc();
386     Map JavaDoc mpConnections = new HashMap JavaDoc();
387     Map JavaDoc mpUsedConnections = new HashMap JavaDoc();
388     List JavaDoc lstConnections = new ArrayList JavaDoc();
389     int iTable1Index = 0;
390     int iIndex;
391     int iCon;
392     StringBuffer JavaDoc sbBuffer = new StringBuffer JavaDoc();
393     System.out.println("RequestConnectionCountOutOfTransaction: Request multiple connections outside of transactions over and over and see if they will be returned in the same order.");
394     try {
395         
396         for (iIndex = 0; iIndex < 10; iIndex++) {
397         try {
398             sbBuffer.delete(0, sbBuffer.length());
399             for (iCon = 0; iCon < 4; iCon++) {
400             conn = spds.getConnection();
401             Assert.assertTrue("XAPool no longer returns instance" +
402                       " of StandardXAConnectionHandle",
403                       conn instanceof StandardXAConnectionHandle);
404             tempCon = (StandardXAConnectionHandle)conn;
405             strCon = tempCon.con.toString();
406             sbBuffer.append(strCon);
407             sbBuffer.append("::");
408             lstConnections.add(conn);
409             mpConnections.put(strCon, strCon);
410             try {
411                 insertStatement = conn.prepareStatement(SQL_QUERY);
412                 insertStatement.setInt(1, iTable1Index++);
413                 iInsertCount = insertStatement.executeUpdate();
414                 Assert.assertEquals("One record should have been inserted.",
415                         1, iInsertCount);
416             } finally {
417                 insertStatement.close();
418                 insertStatement = null;
419             }
420             tempCon = (StandardXAConnectionHandle)conn;
421             strCon = tempCon.con.toString();
422             mpUsedConnections.put(strCon, strCon);
423             
424             // DOn't commit immidiately, commit later so we can simmulate
425
// the jotm transaction behaviour
426
}
427             mpReturnPatternMap.put(sbBuffer.toString(), sbBuffer.toString());
428         } finally {
429             for (iCon = 0; iCon < 4; iCon++) {
430             try {
431                 conn = (Connection JavaDoc)lstConnections.remove(0);
432
433             } catch (Throwable JavaDoc thr) {
434             } finally {
435                 conn.close();
436                 con = null;
437             }
438             }
439         }
440         }
441         /*
442           Log.getInstance().info("testRequestConnectionCountOutOfTransaction =====");
443           Log.getInstance().info("Total allocated connections = "
444           + mpConnections.size());
445           Log.getInstance().info("Total used connections = "
446           + mpUsedConnections.size());
447           Log.getInstance().info("Connections return pattern count = "
448           + mpReturnPatternMap.size());
449           for (Iterator itrElements = mpReturnPatternMap.values().iterator();
450               itrElements.hasNext();)
451           {
452           Log.getInstance().info(itrElements.next().toString());
453           }
454         */

455         Assert.assertEquals("More than expected number of connections were used.",
456                 4, mpUsedConnections.size());
457         Assert.assertEquals("More than expected number of connections were allocated.",
458                 4, mpConnections.size());
459         // Assert.assertEquals("Connections were not returned always in the same order.",
460
// 1, mpReturnPatternMap.size());
461
} finally {
462         /*
463           PreparedStatement deleteStatement = null;
464           
465           m_transaction.begin();
466           try {
467           try {
468           deleteStatement = m_connection.prepareStatement(DELETE_ALL1);
469           int iDeleteCount = DatabaseUtils.executeUpdateAndClose(deleteStatement);
470           } finally {
471           DatabaseUtils.closeStatement(deleteStatement);
472           deleteStatement = null;
473           }
474           m_transaction.commit();
475           } catch (Throwable throwable) {
476           m_transaction.rollback();
477           throw throwable;
478           }
479         */

480     }
481     }
482
483
484
485     public void testMiroRepeatedRequestConnectionInTransaction() throws Throwable JavaDoc {
486     System.out.println("RepeatedRequestConnectionInTransaction: Request one connection over and over in one transactions and see if the same connection will be returned.");
487     try {
488         // Just get connections and return
489
try {
490         StandardXAConnectionHandle tempCon;
491         String JavaDoc strCon;
492         int iInsertCount;
493         PreparedStatement JavaDoc insertStatement = null;;
494         int iTable1Index = 0;
495         int iIndex;
496         int iConChange = 0;
497         String JavaDoc strLastCon = null;
498         Map JavaDoc mpConnections = new HashMap JavaDoc();
499         
500         utx.begin();
501         for (iIndex = 0; iIndex < 10; iIndex++) {
502             try {
503             conn = spds.getConnection();
504             Assert.assertTrue("XAPool no longer returns instance" +
505                       " of StandardXAConnectionHandle",
506                       conn instanceof StandardXAConnectionHandle);
507             tempCon = (StandardXAConnectionHandle)conn;
508             strCon = tempCon.con.toString();
509             if (strLastCon == null) {
510                 strLastCon = strCon;
511             } else {
512                 if (!strCon.equals(strLastCon)) {
513                 // The connection has changed, count how many times
514
// it has changed
515
strLastCon = strCon;
516                 iConChange++;
517                 }
518             }
519             mpConnections.put(strCon, strCon);
520             
521             // Don't do anything here, just get and return
522
}
523             finally {
524             conn.close();
525             conn = null;
526             }
527         }
528         /*
529           Log.getInstance().info("testRepeatedRequestConnectionInTransaction =====");
530           for (Iterator itrElements = mpConnections.values().iterator();
531           itrElements.hasNext();)
532           {
533           Log.getInstance().info(itrElements.next().toString());
534           }
535           Log.getInstance().info("Total allocated connections = "
536           + mpConnections.size());
537           Log.getInstance().info("How many times pool returned different connection = "
538           + iConChange);
539         */

540         Assert.assertEquals("More than expected number of connections were allocated.",
541                     1, mpConnections.size());
542         
543         utx.commit();
544         } catch (Throwable JavaDoc throwable) {
545         utx.rollback();
546         throw throwable;
547         }
548     } finally {
549         /*
550           PreparedStatement deleteStatement = null;
551           
552           m_transaction.begin();
553           try {
554           try {
555           deleteStatement = m_connection.prepareStatement(DELETE_ALL1);
556           int iDeleteCount = DatabaseUtils.executeUpdateAndClose(deleteStatement);
557           } finally {
558           DatabaseUtils.closeStatement(deleteStatement);
559           deleteStatement = null;
560           }
561           m_transaction.commit();
562           } catch (Throwable throwable) {
563           m_transaction.rollback();
564           throw throwable;
565           }
566         */

567     }
568     }
569
570
571     
572     public void testMiroRepeatedRequestConnectionWithSelectInTransaction() throws Throwable JavaDoc {
573     System.out.println("RepeatedRequestConnectionWithSelectInTransaction: Request one connection over and over in one transactions and do select and then see if the same connection will be returned and used");
574     try {
575         // Get connection, select and return
576
try {
577         StandardXAConnectionHandle tempCon;
578         String JavaDoc strCon;
579         PreparedStatement JavaDoc selectStatement = null;
580         int iTable1Index = 0;
581         int iIndex;
582         int iConChange = 0;
583         String JavaDoc strLastCon = null;
584         ResultSet JavaDoc rsResults = null;
585         Map JavaDoc mpConnections = new HashMap JavaDoc();
586         Map JavaDoc mpUsedConnections = new HashMap JavaDoc();
587         
588         utx.begin();
589         for (iIndex = 0; iIndex < 10; iIndex++) {
590             try {
591             conn = spds.getConnection();
592             Assert.assertTrue("XAPool no longer returns instance" +
593                       " of StandardXAConnectionHandle",
594                       conn instanceof StandardXAConnectionHandle);
595             tempCon = (StandardXAConnectionHandle)conn;
596             strCon = tempCon.con.toString();
597             if (strLastCon == null) {
598                 strLastCon = strCon;
599             } else {
600                 if (!strCon.equals(strLastCon)) {
601                 // The connection has changed, count how many times
602
// it has changed
603
strLastCon = strCon;
604                 iConChange++;
605                 }
606             }
607             mpConnections.put(strCon, strCon);
608             
609             // Now try to select using this connection
610
try {
611                 selectStatement = conn.prepareStatement(SQL_REQUEST);
612                 rsResults = selectStatement.executeQuery();
613                 rsResults.close();
614             } finally {
615
616                 selectStatement.close();
617                 rsResults = null;
618                 selectStatement = null;
619             }
620             
621             tempCon = (StandardXAConnectionHandle)conn;
622             strCon = tempCon.con.toString();
623             mpUsedConnections.put(strCon, strCon);
624             } finally {
625             conn.close();
626             conn = null;
627             }
628         }
629         /*
630           Log.getInstance().info("testRepeatedRequestConnectionInTransaction =====");
631           Log.getInstance().info("Total allocated connections when selecting = "
632           + mpConnections.size());
633           for (Iterator itrElements = mpConnections.values().iterator();
634           itrElements.hasNext();)
635           {
636           Log.getInstance().info(itrElements.next().toString());
637           }
638           Log.getInstance().info("Total used connections when selecting = "
639           + mpUsedConnections.size());
640           for (Iterator itrElements = mpUsedConnections.values().iterator();
641           itrElements.hasNext();)
642           {
643           Log.getInstance().info(itrElements.next().toString());
644           }
645           Log.getInstance().info("How many times pool returned different connection" +
646           " when selecting = " + iConChange);
647         */

648         Assert.assertEquals("More than expected number of connections were used" +
649                     " when selecting.", 1, mpUsedConnections.size());
650         // This really doesn't mean anything, two connections were taken from the pool
651
// but only 1 was used so that should be OK
652
// Assert.assertEquals("More than expected number of connections were allocated" +
653
// " when selecting.", 1, mpConnections.size());
654

655         utx.commit();
656         } catch (Throwable JavaDoc throwable) {
657         utx.rollback();
658         throw throwable;
659         }
660     } finally {
661         /*
662           PreparedStatement deleteStatement = null;
663           
664           m_transaction.begin();
665           try
666           {
667           try
668           {
669           deleteStatement = m_connection.prepareStatement(DELETE_ALL1);
670           int iDeleteCount = DatabaseUtils.executeUpdateAndClose(deleteStatement);
671           }
672           finally
673           {
674           DatabaseUtils.closeStatement(deleteStatement);
675           deleteStatement = null;
676           }
677           m_transaction.commit();
678           }
679           catch (Throwable throwable)
680           {
681           m_transaction.rollback();
682           throw throwable;
683           }
684         */

685     }
686     }
687
688     
689     public void testMiroRepeatedRequestConnectionWithSelectOutOfTransaction() throws Throwable JavaDoc {
690     System.out.println("RepeatedRequestConnectionWithSelectOutOfTransaction: Request one connection over and over without transactions and do select and then see if the same connection will be returned and used");
691     try {
692         StandardXAConnectionHandle tempCon;
693         String JavaDoc strCon;
694         PreparedStatement JavaDoc selectStatement = null;;
695         int iTable1Index = 0;
696         int iIndex;
697         int iConChange = 0;
698         String JavaDoc strLastCon = null;
699         ResultSet JavaDoc rsResults = null;
700         Map JavaDoc mpConnections = new HashMap JavaDoc();
701         Map JavaDoc mpUsedConnections = new HashMap JavaDoc();
702         
703         for (iIndex = 0; iIndex < 10; iIndex++) {
704         try {
705             conn = spds.getConnection();
706             Assert.assertTrue("XAPool no longer returns instance" +
707                       " of StandardXAConnectionHandle",
708                       conn instanceof StandardXAConnectionHandle);
709             tempCon = (StandardXAConnectionHandle)conn;
710             strCon = tempCon.con.toString();
711             if (strLastCon == null) {
712             strLastCon = strCon;
713             } else {
714             if (!strCon.equals(strLastCon)) {
715                 // The connection has changed, count how many times
716
// it has changed
717
strLastCon = strCon;
718                 iConChange++;
719             }
720             }
721             mpConnections.put(strCon, strCon);
722             
723             // Now try to select using this connection
724
try {
725             selectStatement = conn.prepareStatement(SQL_REQUEST);
726             rsResults = selectStatement.executeQuery();
727             rsResults.close();
728             } finally {
729
730             selectStatement.close();
731
732             rsResults = null;
733             selectStatement = null;
734             }
735             
736             tempCon = (StandardXAConnectionHandle)conn;
737             strCon = tempCon.con.toString();
738             mpUsedConnections.put(strCon, strCon);
739         } finally {
740             conn.close();
741             conn = null;
742         }
743         }
744         /*
745           Log.getInstance().info("testRepeatedRequestConnectionWithSelectOutOfTransaction =====");
746           Log.getInstance().info("Total allocated connections when selecting = "
747           + mpConnections.size());
748           for (Iterator itrElements = mpConnections.values().iterator();
749           itrElements.hasNext();)
750           {
751           Log.getInstance().info(itrElements.next().toString());
752           }
753           Log.getInstance().info("Total used connections when selecting = "
754           + mpUsedConnections.size());
755           for (Iterator itrElements = mpUsedConnections.values().iterator();
756           itrElements.hasNext();)
757           {
758           Log.getInstance().info(itrElements.next().toString());
759           }
760           Log.getInstance().info("How many times pool returned different connection" +
761           " when selecting = " + iConChange);
762         */

763         Assert.assertEquals("More than expected number of connections were used" +
764                 " when selecting.", 1, mpUsedConnections.size());
765         Assert.assertEquals("More than expected number of connections were allocated" +
766                 " when selecting.", 1, mpConnections.size());
767     } finally {
768         /*
769           PreparedStatement deleteStatement = null;
770           
771           m_transaction.begin();
772           try
773           {
774           try
775           {
776           deleteStatement = m_connection.prepareStatement(DELETE_ALL1);
777           int iDeleteCount = DatabaseUtils.executeUpdateAndClose(deleteStatement);
778           }
779           finally
780           {
781           DatabaseUtils.closeStatement(deleteStatement);
782           deleteStatement = null;
783           }
784           m_transaction.commit();
785           }
786           catch (Throwable throwable)
787           {
788           m_transaction.rollback();
789           throw throwable;
790           }
791         */

792     }
793     }
794     
795
796     public void testMiroRepeatedRequestConnectionWithInsertInTransaction() throws Throwable JavaDoc {
797     System.out.println("RepeatedRequestConnectionWithInsertInTransaction: Request one connection over and over in one transactions and do insert and then see if the same connection will be returned and used");
798     try {
799         try {
800         StandardXAConnectionHandle tempCon;
801         String JavaDoc strCon;
802         int iInsertCount;
803         PreparedStatement JavaDoc insertStatement = null;;
804         int iTable1Index = 0;
805         int iIndex;
806         int iConChange = 0;
807         String JavaDoc strLastCon = null;
808         Map JavaDoc mpConnections = new HashMap JavaDoc();
809         Map JavaDoc mpUsedConnections = new HashMap JavaDoc();
810         
811         utx.begin();
812         for (iIndex = 0; iIndex < 10; iIndex++) {
813             try {
814             conn = spds.getConnection();
815             Assert.assertTrue("XAPool no longer returns instance" +
816                       " of StandardXAConnectionHandle",
817                       conn instanceof StandardXAConnectionHandle);
818             tempCon = (StandardXAConnectionHandle)conn;
819             strCon = tempCon.con.toString();
820             if (strLastCon == null) {
821                 strLastCon = strCon;
822             } else {
823                 if (!strCon.equals(strLastCon)) {
824                 // The connection has changed, count how many times
825
// it has changed
826
strLastCon = strCon;
827                 iConChange++;
828                 }
829             }
830             mpConnections.put(strCon, strCon);
831             
832             // Now try to insert using this connection
833
try {
834                 insertStatement = conn.prepareStatement(SQL_QUERY);
835                 insertStatement.setInt(1, iTable1Index++);
836                 iInsertCount = insertStatement.executeUpdate();
837                 Assert.assertEquals("One record should have been inserted.",
838                         1, iInsertCount);
839             } finally {
840                 insertStatement.close();
841                 insertStatement = null;
842             }
843             
844             tempCon = (StandardXAConnectionHandle)conn;
845             strCon = tempCon.con.toString();
846             mpUsedConnections.put(strCon, strCon);
847             } finally {
848             conn.close();
849             conn = null;
850             }
851         }
852         /*
853           Log.getInstance().info("testRepeatedRequestConnectionWithInsertInTransaction =====");
854           Log.getInstance().info("Total allocated connections when inserting = "
855           + mpConnections.size());
856           for (Iterator itrElements = mpConnections.values().iterator();
857           itrElements.hasNext();)
858           {
859           Log.getInstance().info(itrElements.next().toString());
860           }
861           Log.getInstance().info("Total used connections when inserting = "
862           + mpUsedConnections.size());
863           for (Iterator itrElements = mpUsedConnections.values().iterator();
864           itrElements.hasNext();)
865           {
866           Log.getInstance().info(itrElements.next().toString());
867           }
868           Log.getInstance().info("How many times pool returned different connection" +
869           " when inserting = " + iConChange);
870         */

871         Assert.assertEquals("More than expected number of connections were used" +
872                     " when inserting.", 1, mpUsedConnections.size());
873         // This really doesn't mean anything, two connections were taken from the pool
874
// but only 1 was used so that should be OK
875
// Assert.assertEquals("More than expected number of connections were allocated" +
876
// " when inserting.", 1, mpConnections.size());
877

878         utx.commit();
879         } catch (Throwable JavaDoc throwable) {
880         utx.rollback();
881         throw throwable;
882         }
883     } finally {
884         /*
885           PreparedStatement deleteStatement = null;
886           
887           m_transaction.begin();
888           try
889           {
890           try
891           {
892           deleteStatement = m_connection.prepareStatement(DELETE_ALL1);
893           int iDeleteCount = DatabaseUtils.executeUpdateAndClose(deleteStatement);
894           }
895           finally
896           {
897           DatabaseUtils.closeStatement(deleteStatement);
898           deleteStatement = null;
899           }
900           m_transaction.commit();
901           }
902           catch (Throwable throwable)
903           {
904           m_transaction.rollback();
905           throw throwable;
906           }
907         */

908     }
909     }
910     
911     public void testMiroInsertSelectInTransaction() throws Throwable JavaDoc {
912     System.out.println("InsertSelectInTransaction: We try to simulate situation, when xapool returns different connections for first and second request inside of the same transaction and see if it affects functionality.");
913     try {
914         Map JavaDoc mpConnections = new HashMap JavaDoc();
915         Map JavaDoc mpUsedConnections = new HashMap JavaDoc();
916         int iConChange = 0;
917         
918         try {
919         StandardXAConnectionHandle tempCon;
920         String JavaDoc strCon;
921         int iInsertCount;
922         PreparedStatement JavaDoc insertStatement = null;
923         PreparedStatement JavaDoc selectStatement = null;
924         ResultSet JavaDoc rsResults = null;
925         int iTable1Index = 0;
926         int iIndex;
927         String JavaDoc strLastCon = null;
928         
929         utx.begin();
930         
931         try {
932             conn = spds.getConnection();
933             Assert.assertTrue("XAPool no longer returns instance" +
934                       " of StandardXAConnectionHandle",
935                       conn instanceof StandardXAConnectionHandle);
936             tempCon = (StandardXAConnectionHandle)conn;
937             strCon = tempCon.con.toString();
938             if (strLastCon == null) {
939             strLastCon = strCon;
940             } else {
941             if (!strCon.equals(strLastCon)) {
942                 // The connection has changed, count how many times
943
// it has changed
944
strLastCon = strCon;
945                 iConChange++;
946             }
947             }
948             mpConnections.put(strCon, strCon);
949             
950             // Now try to insert using this connection
951
try {
952             insertStatement = conn.prepareStatement(SQL_QUERY);
953             insertStatement.setInt(1, iTable1Index++);
954             iInsertCount = insertStatement.executeUpdate();
955             Assert.assertEquals("One record should have been inserted.",
956                         1, iInsertCount);
957             } finally {
958             insertStatement.close();
959             insertStatement = null;
960             }
961             
962             tempCon = (StandardXAConnectionHandle)conn;
963             strCon = tempCon.con.toString();
964             mpUsedConnections.put(strCon, strCon);
965         } finally {
966             conn.close();
967             conn = null;
968         }
969         
970         try {
971             conn = spds.getConnection();
972             Assert.assertTrue("XAPool no longer returns instance" +
973                       " of StandardXAConnectionHandle",
974                       conn instanceof StandardXAConnectionHandle);
975             tempCon = (StandardXAConnectionHandle)conn;
976             strCon = tempCon.con.toString();
977             if (strLastCon == null) {
978             strLastCon = strCon;
979             } else {
980             if (!strCon.equals(strLastCon)) {
981                 // The connection has changed, count how many times
982
// it has changed
983
strLastCon = strCon;
984                 iConChange++;
985             }
986             }
987             mpConnections.put(strCon, strCon);
988             
989             // Now try to select using this connection
990
try {
991             selectStatement = conn.prepareStatement(SQL_REQUEST);
992             rsResults = selectStatement.executeQuery();
993             Assert.assertTrue("Select didn't find inserted record in the database.",
994                       rsResults.next());
995             } finally {
996             rsResults.close();
997             selectStatement.close();
998             rsResults = null;
999             selectStatement = null;
1000            }
1001            
1002            tempCon = (StandardXAConnectionHandle)conn;
1003            strCon = tempCon.con.toString();
1004            mpUsedConnections.put(strCon, strCon);
1005        } finally {
1006            conn.close();
1007            conn = null;
1008        }
1009        
1010        utx.commit();
1011        } catch (Throwable JavaDoc throwable) {
1012        utx.rollback();
1013        throw throwable;
1014        }
1015        /*
1016          Log.getInstance().info("testInsertSelectInTransaction =====");
1017          Log.getInstance().info("Total allocated connections when insert/select = "
1018          + mpConnections.size());
1019          for (Iterator itrElements = mpConnections.values().iterator();
1020          itrElements.hasNext();)
1021          {
1022          Log.getInstance().info(itrElements.next().toString());
1023          }
1024          Log.getInstance().info("Total used connections when insert/select = "
1025          + mpUsedConnections.size());
1026          for (Iterator itrElements = mpUsedConnections.values().iterator();
1027          itrElements.hasNext();)
1028          {
1029          Log.getInstance().info(itrElements.next().toString());
1030          }
1031          Log.getInstance().info("How many times pool returned different connection" +
1032          " when insert/select = " + iConChange);
1033        */

1034        Assert.assertEquals("More than expected number of connections were used" +
1035                " when insert/select.", 1, mpUsedConnections.size());
1036        // This really doesn't mean anything, two connections were taken from the pool
1037
// but only 1 was used so that should be OK
1038
// Assert.assertEquals("More than expected number of connections were allocated" +
1039
// " when insert/select.", 1, mpConnections.size());
1040
} finally {
1041        /*
1042          PreparedStatement deleteStatement = null;
1043        
1044          m_transaction.begin();
1045          try
1046          {
1047          try
1048          {
1049          deleteStatement = m_connection.prepareStatement(DELETE_ALL1);
1050          int iDeleteCount = DatabaseUtils.executeUpdateAndClose(deleteStatement);
1051          }
1052          finally
1053          {
1054          DatabaseUtils.closeStatement(deleteStatement);
1055          deleteStatement = null;
1056          }
1057          m_transaction.commit();
1058          }
1059          catch (Throwable throwable)
1060          {
1061          m_transaction.rollback();
1062          throw throwable;
1063          }
1064        */

1065    }
1066    }
1067    
1068    
1069    public void testMiroEmptyRequestReturnConnection() throws Throwable JavaDoc {
1070    System.out.println("EmptyRequestReturnConnection: This test triggers bug in out modified XAPool 1.3.1 which doesn't occur with latest the original XAPool or the latest XAPool.");
1071    
1072    Connection JavaDoc con1 = null;
1073    Connection JavaDoc con2 = null;
1074    StandardXAConnectionHandle tempCon;
1075    String JavaDoc strCon1;
1076    String JavaDoc strCon2;
1077    int iInsertCount;
1078    
1079    PreparedStatement JavaDoc insertStatement = null;;
1080    PreparedStatement JavaDoc selectStatement = null;;
1081    ResultSet JavaDoc rsResults = null;
1082    
1083    try {
1084         try {
1085         utx.begin();
1086         final int VALUE_TEST_1 = 0;
1087         Map JavaDoc mpConnectionMap = new HashMap JavaDoc();
1088         
1089         // Request a connection insert a record and return it to the pool
1090
try {
1091         con1 = spds.getConnection();
1092         Assert.assertTrue("XAPool no longer returns instance of StandardXAConnectionHandle",
1093                   con1 instanceof StandardXAConnectionHandle);
1094         tempCon = (StandardXAConnectionHandle)con1;
1095         strCon1 = tempCon.con.toString();
1096         mpConnectionMap.put(strCon1, new Integer JavaDoc(0));
1097         
1098         // Inserted record 0
1099
insertStatement = con1.prepareStatement(SQL_QUERY);
1100         insertStatement.setInt(1, VALUE_TEST_1);
1101         iInsertCount = insertStatement.executeUpdate();
1102         Assert.assertEquals("One record should have been inserted.", 1, iInsertCount);
1103         } finally {
1104         insertStatement.close();
1105         insertStatement = null;
1106         con1.close();
1107         con1 = null;
1108         }
1109         
1110         boolean bDetectedTheSame = false;
1111         int iIndex;
1112         
1113         // Now request a connection from the pool, if it is a connection which
1114
// was already requested before, just return it to the pool.
1115
// -----------------------------------------------------------------
1116
// This should trigger bug in our modified XAPool 1.3.1 which will cause rollback
1117
// on the record inserted before using the same connection.
1118
// -----------------------------------------------------------------
1119
// If it is not the same connection, then insert new record and return
1120
// connection to the pool.
1121
// Loop till 100 which is hopefully less than max connection count
1122
// therefore we should get at some point the same connection
1123
for (iIndex = 1; iIndex < 100; iIndex++) {
1124         try {
1125             con2 = spds.getConnection();
1126             Assert.assertTrue("XAPool no longer returns instance"
1127                       + " of StandardXAConnectionHandle",
1128                       con2 instanceof StandardXAConnectionHandle);
1129             tempCon = (StandardXAConnectionHandle)con2;
1130             strCon2 = tempCon.con.toString();
1131             
1132             if (mpConnectionMap.get(strCon2) == null) {
1133             // This is highly undesirable, since it can cause deadlock,
1134
// but I will let it be for now and use other test to deal with it
1135

1136             // Inserted record 1, 2, ...
1137
insertStatement = con2.prepareStatement(SQL_QUERY);
1138             insertStatement.setInt(1, iIndex);
1139             iInsertCount = insertStatement.executeUpdate();
1140             Assert.assertEquals("One record should have been inserted.", 1, iInsertCount);
1141             } else {
1142             // Retrieve the index which was already inserted
1143
iIndex = ((Integer JavaDoc)mpConnectionMap.get(strCon2)).intValue();
1144             bDetectedTheSame = true;
1145             // Just let it close the connection without doing anything
1146
// -----------------------------------------------------------------
1147
// This should trigger bug in our modified XAPool 1.3.1 which will
1148
// cause rollback on the record inserted before using the same connection.
1149
// -----------------------------------------------------------------
1150
break;
1151             }
1152             mpConnectionMap.put(strCon2, new Integer JavaDoc(iIndex));
1153         } finally {
1154             if (insertStatement != null)
1155             insertStatement.close();
1156             insertStatement = null;
1157             con2.close();
1158             con2 = null;
1159         }
1160         }
1161             
1162         Assert.assertTrue("Unable to get the same connection from the pool.",
1163                   bDetectedTheSame);
1164         
1165         // -----------------------------------------------------------------
1166
// This detects bug in our modified XAPool 1.3.1 since the inserted record is
1167
// not there
1168
// -----------------------------------------------------------------
1169
try {
1170         con1 = spds.getConnection();
1171         selectStatement = con1.prepareStatement(SQL_QUERY);
1172         selectStatement.setInt(1, iIndex);
1173         try {
1174             rsResults = selectStatement.executeQuery();
1175             Assert.assertTrue("Canot find inserted record with index " + iIndex,
1176                       rsResults.next());
1177             rsResults.close();
1178           
1179         } catch (Exception JavaDoc e) {
1180             // in case rsReults contains no data
1181
}
1182         } finally {
1183
1184         selectStatement.close();
1185         selectStatement = null;
1186         con1.close();
1187         con1 = null;
1188         }
1189         
1190         utx.commit();
1191         } catch (Throwable JavaDoc throwable) {
1192         utx.rollback();
1193         throw throwable;
1194         }
1195    } finally {
1196        utx.begin();
1197        try {
1198        conn = spds.getConnection();
1199        /*
1200          PreparedStatement deleteStatement = conn.prepareStatement(DELETE_ALL);
1201          
1202          int iDeleteCount = DatabaseUtils.executeUpdateAndClose(deleteStatement);
1203        */

1204        conn.close();
1205        utx.commit();
1206        } catch (Throwable JavaDoc throwable) {
1207        utx.rollback();
1208        throw throwable;
1209        }
1210    }
1211    }
1212    
1213}
1214
Popular Tags