From: Bruce M. <bmc...@in...> - 2001-09-24 20:14:42
|
Hi Kevin, I believe you are trying set something that doesn't exist yet. In the first iteration, you are setting conlist[0], but conlist[0] doesn't exist yet. As suggested previously, you need to append the values to the list, then you can reference them. Thx, Bruce -----Original Message----- From: Kevin Butler [mailto:kb...@ca...] Sent: Monday, September 24, 2001 1:02 PM To: R Datta Cc: jyt...@li... Subject: Re: [Jython-users] List issue R Datta wrote: > 40 conlist= [] > 41 for i in xrange(threadCount): > 42 url = "jdbc:mysql://%s/%s?user=%s&password=%s" % (server, db, > usr, passwd) > 43 print "Connection # ",i > 44 conlist[i]=DriverManager.getConnection(url) > 45 sleep(0.5) > > It gives me the following error: > Connection # 0 > Traceback (innermost last): > File "cxjdbcThread1.py", line 44, in ? > IndexError: index out of range: -1 > > I must be making some very obvious mistake, but I can't find it. Yes. :-) There is no item at conlist[0] to replace via that assignment. Try "conlist.append( ... )" or "conlist.insert( 0, ... )" > If there > is a better (more efficient) way of storing the connection objects, pls > tell me. If you're just talking memory efficiency, you may want to put them in a dictionary (Map), with the thread that is using them as a key. You may want to use a Java WeakHashMap to ensure that threads & connections can be garbage collected when the thread terminates. If you want time efficiency, you probably don't want the sleep() or the print. And you should move the string formatting outside the loop, and possibly store a local reference to the append method: a = conlist.append for... a( DriverManager.getConnection(url) ) If you really want to max it out, you could do something like the following: url = "jdbc:mysql://%s/%s?user=%s&password=%s" % (server, db, usr, passwd) conlist = [url]*threadCount conlist = map( DriverManager.getConnection, conlist ) This builds the URL once, and moves the iteration & method invocation into native Java code, but has the cost of creating two lists of length=threadCount, instead of just one. Of course, this type of micro-optimization will be completely masked by the overhead of creating JDBC connections, but it may be applicable in other code... YMMV. kb _______________________________________________ Jython-users mailing list Jyt...@li... https://lists.sourceforge.net/lists/listinfo/jython-users |