crystal-lang


What is the right way to work with DB and DB connection pool?


In my code I open DB at start of program and pass db variable to other methods. I think it's stupid and not right. But what should I do? Should I open db connection in each method? But this way also doesn't look right... And I have a lot of errors: DB::ConnectionRefused, DB::PoolTimeout, DB::PoolRetryAttemptsExceeded
So it something wrong with my code.
def main_meth
db = DB.open("postgres://blabla#localhost:5432/bla?retry_attempts=8&retry_delay=3&max_pool_size=50&max_idle_pool_size=10&checkout_timeout=0.1") # there is always same story with or without params.
begin
db.scalar("")
...
another_meth(params, db)
channel = Channel(Nil).new(20)
groups.each do |group|
spawn one_more_meth(group, channel, db)
end
groups.size.times { channel.receive }
ensure
db.close
end
end
def another_meth(p, db)
deeper_meth(db)
end
def one_more_meth(group, channel, db)
...
db.query_all
...
channel.send(nil)
end
I ran into a similar issue and I found is that when doing db.query you have to make sure to either:
save the result sets to a variable and after usage close them
rs = db.query("")
Class.from_rs(rs)
rs.close
OR use a block
db.query("") do |rs|
Class.from_rs(rs)
end

Related Links

Crystal: abstract static method
Type checking in Crystal
Condition over object's type
Crystal C bindings: argument const unsigned char **
Crystal C bindings: return type char*
Make Class member attribute of type Array(T) accept 2D arrays of T
JSON.mapping if “root” attribute has inherit keys
Crystal C bindings: using fopen
Define Class object validations
Set attribute of type “Class” to define it as Int32, String, Float64
Set both default attribute and type of attribute
How can I test private methods?
Write binary data to stdout in Crystal
Unable to compile after upgrade from crystal 0.19.4 to 0.20
Application server for Crystal web app / Kemal
Crystal Fiber error - “Unhandled exception in spawn: SSL_shutdown: Operation now in progress (Errno)”

Categories

HOME
windows
google-app-engine
embedded
oracle12c
gridview
codenvy
twig
toolbox
interface
themes
sh
opendj
database-normalization
regular-language
interpolation
google-cloud-datalab
vert.x
plist
windows-ce
automatic-ref-counting
speech-synthesis
radgridview
parameter-passing
git-extensions
nodemcu
dapper-extensions
jquery-multiselect
apktool
8051
comparable
dotspatial
panoramas
postback
memory-address
android-sugarorm
card.io
sammy.js
getpixel
processwire
video-capture
engine.io
apiary.io
google-earth-engine
recurrent-neural-network
google-data-api
setup.py
naming
dbnull
ssrs-2014
tcc
signal-strength
stylecop
plan-9
fwrite
term
openrasta
hammer.js
user-defined-fields
mschart
dtsearch
datamaps
spring-data-couchbase
filehandle
django-1.10
cvxpy
taocp
simplepie
cubism.js
burrows-wheeler-transform
uicolor
jodd
synonym
hclust
deep
lz77
libav
findfirst
aspen
kendo-upload
blitline
contentcontrol
design-principles
phpgrid
sciruby
avisynth
fuelcms
phonertc
mod-proxy
eoferror
apache-commons-daemon
typeconverter
exceldatareader
pseudo-class
formview
reflector
lnk
gnumeric
extjs2
vbe
menubar
syndicationfeed
xmltodict
mysql-error-1044
breakout
ui-select2
flex++
poker
winmain
dataform
cdonts
django-paypal
commerceserver2007
ie-compatibility-mode
hbm2java
addchild
data-mapping
fdf
application-planning
soappy

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App