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

Read a single char from stdin without pressing enter
What object receives #puts in my Crystal programs?
Avoid repeating variable name in assignation
Sharing variables between different macros
passing json to a superclass using .from_json
execution of command failed with code: 1: `cc -o "/root/.cache/crystal/var-app-staging-new-http.cr
How to use union in “if” statement [Crystal]
How to convert a hex string to bytes array in Crystal?
is INSTANCE = new in class is run after compilation automatically or the first time it is accessed?
Crystal lang: understanding method return type of Void
Crystal-lang httpget basic_auth
How to control using CPU cores [Crystal]
How can I read a slice with other size in crystal?
Unix-style password readline
Cannot find -lxml2 when trying to run crystal spec
How do I access an object's eigenclass in Crystal?

Categories

HOME
delphi-7
apache-flink
apache-jena
pagination
object
static
bitbucket-api
switch-statement
permissions
special-characters
extjs4.2
accessibility
ibeacon-android
fhir
java.util.logging
alamofire
dimensional-modeling
forgot-password
nim
microsoft-ocr
dpdk
epsilon
jboss6.x
tiki-wiki
pylons
entity-relationship-model
firebase-analytics
datastax-enterprise-graph
iis-express
nsurl
gitlab-api
arduino-ide
browser-extension
processwire
openproject
trigonometry
skylink
branching-and-merging
postgresql-8.4
android-ibeacon
preloader
memory-mapping
flask-login
strftime
lampp
jquery-ui-draggable
semantic-logging
colorbar
dt
er-diagram
atlas
myspace
i386
dimension
clipboard.js
nss
data-protection
realsense
concept-insights
concurrentmodification
terracotta
zen-cart
atoi
viola-jones
apache-commons-daemon
jquery-forms-plugin
fits
punycode
trdion2011
alternate
radiant
eclipse-kepler
script#
scringo
neos-server
process-explorer
diem
sqlclr
web-farm
agility.js
scrollbars
cldc
dsoframer
soappy
ifilter
appliance
commercial-application
pnrp

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App