Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

poll connection in tdbc

33 views
Skip to first unread message

MaxJarek

unread,
Jun 22, 2012, 6:55:00 AM6/22/12
to
Hi, all

tdbc layer don't support pool db connection. I prepare this script for test.

package require tdbc
package require tdbc::sqlite3
package require struct::pool

set maxconn 10
struct::pool dbPool $maxconn

# open new db connections
for {set i 0} {$i < $maxconn} {incr i} {
tdbc::sqlite3::connection create db($i) test.db
dbPool add db($i)
}

# defining statements for db conncetions
foreach conn [dbPool info allitems] {
set GetUserByID($conn) [$conn prepare {select * from clients where id=:id}]
}

# get free db connection
while {1} {
if {[incr l] > 200} {exit}
if {[dbPool request conn]} {break}
after 100
}

puts [$GetUserByID($conn) allrows -as dicts [list id 100]]

# release db connection
dbPool release $conn

# close all db connections
foreach conn [dbPool info allitems] {
$conn close
}


But i don't know this is right way. Any sugestions? Of course sqlite is only for testing.

<MaxJarek>

Harald Oehlmann

unread,
Jun 22, 2012, 9:13:51 AM6/22/12
to
Hi Max,

if you want to test, if a db connection is still alive, one may issue
a "db configure" option.

Here is a code snippet:
proc ::wwwbase::getDbConnection {} {
set args {-user me -passwd my -host 127.0.0.1 -database products}
if {[info commands ::db] ne ""} {
## Already have a connection.
if {![catch {::db configure {*}$args} error]} {
return 0
}
catch {::db close}
}

# > Catch to not show the password in the log
if {[catch {
tdbc::mysql::connection create ::db {*}$args
} Err]} {
return -code error "No data base connection"
}
return 1
}
0 new messages