jordan0day

jordan0day

How to properly use Mariaex / DBConnection?

I have an application which I’m using Mariaex to query a MySQL db, but I’m not using Ecto. I’m curious what the proper way is to handle connection/instances of Mariaex for a long-running application? Under the hood, Mariaex uses DBConnection to create the connection and do all the connection pooling stuff, but I’m unsure if I’m supposed to just start one instance of Mariaex (via start_link) and hold on to that pid for the duration of the application (ultimately I’m sure I’d use a supervisor), or if I should be calling Mariaex.start_link/1 each time I want to perform a new query? If the latter, do I manually stop the pid returned by Mariaex.start_link/1 when the query has completed? Does DBConnection handle shutting it down?

I’ve looked through Mariaex, DBConnection, and Ecto’s codebases to try to understand how it’s handled in Ecto, but there’s a sufficient amount of abstraction and indirection that I haven’t really been able to tell how Ecto handles this.

I’d appreciate any advice anyone could provide on this.

** UPDATE **
Sorry, I guess I shouldn’t have posted this on a day when I was loaded up on cold medicine. I now see that I had forgotten to set the pool to DBConnection.Poolboy or DBConnection.Sojourn, (though in observer there was a DBConnection.Sojourn.Supervisor started, so I naively assumed sojourn was being used by default), and had confused myself about the connection behavior.

Most Liked

hubertlepicki

hubertlepicki

Hey, I had this blog post as draft for quite some time now, just published it for you:

is this helpful?

acrolink

acrolink

Absolutely, it was the very piece of code I was looking for :slight_smile: (I mean, using Supervisor and Poolboy). For my current little project, maximum 3 people would be interacting with the database at the same time, but I guess it is still better to set the connection up and let Poolboy handle the connections. Thank you.

jordan0day

jordan0day

It’s in the options you pass to Mariaex.start_link/1, Mariaex.query/4, and again in the options passed to Mariaex.transaction/3. See https://github.com/xerions/mariaex/blob/master/lib/mariaex.ex#L333

Hope this helps!

nichochar

nichochar

Thanks this is exactly what I was looking for.

Where Next?

Popular in Questions Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement