[erlang-questions] Newbie Question: Module with DB Connection as a "global state"
Wed Oct 5 16:57:58 CEST 2011
The way you could do it is to have the log_packet_send/3 function make a
callback so you can handle the request in a context where you have the
state defined in the init function. I haven't used gen_mod so I don't
know exactly how that works, but I assume it works in a similar way to
how a gen_server implementation would call Module:handle_cast/2 for
Note, for instance, how you have access to the state when a info message
is received in handle_info/2 of your module.
Also the proper syntax for accessing record fields is Expr#Name.Field
which would translate to DBref = State#state.dbref in your case.
I hope that clarifies things a little :)
On Wed, 2011-10-05 at 16:26 +0200, Michael Weibel - Amiado Group wrote:
> Hi Gustav,
> Wow, thanks for your _quick_ reply! :)
> Yeah at first I thought also that I could use that but didn't really figure out how to do that (maybe I don't really understand records yet).
> Because the "log_packet_send" is called from ejabberd (it's a hook) and from this function, as you see, I call some other functions until it arrives at the "insert_row" function.
> In the insert_row function I'd need the DB Reference as you see.
> There, can I just use it like this:
> DBref = #state.dbref
> Is this the correct way?
> Am 05.10.2011 um 15:54 schrieb Gustav Simonsson:
> > Hi Michael!
> > On line 48 in your code you store the connection information used in
> > open_mysql_connection/1 in the state of the server. Perhaps you can
> > make use of that?
> > Regards,
> > Gustav Simonsson
> > On Wed, 2011-10-05 at 15:36 +0200, Michael Weibel - Amiado Group wrote:
> >> Hi all,
> >> I'm new to erlang, new to this list and I hope here's the right place to ask this question.
> >> I'm currently implementing a module for ejabberd where I need to log certain packets to a MySQL DB and I'm using the MySQL Native Driver for it.
> >> My current implementation is on github:
> >> https://github.com/amiadogroup/mod_log_chat_mysql5/blob/master/src/mod_log_chat_mysql5.erl
> >> In this file you see that I'm opening the MySQL Connection in the function "open_mysql_connection" and assign it to a ETS Table because I didn't find another way to store the DB Reference (I tried something with records but didn't succeed).
> >> This worked pretty well but I ran into the problem that after some days/weeks the ets table didn't have the DB Reference anymore.
> >> To prevent this, I want to create functions which setup the DB Connection again, if it doesn't have it anymore.
> >> As I'm trying to do this I run into the problem that the informations about how to connect to the db is only in the init-function available and I don't really want to store this information also in the ets table.
> >> Can anyone help me or point me to a site on how to do this?
> >> Thanks,
> >> Michael
> >> _______________________________________________
> >> erlang-questions mailing list
> >> erlang-questions@REDACTED
> >> http://erlang.org/mailman/listinfo/erlang-questions
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
More information about the erlang-questions