comp.lang.ada
 help / color / mirror / Atom feed
* Ada and MySQL (mysql_library_init)
@ 2009-12-05 22:41 Tomek Walkuski
  2009-12-05 23:03 ` Maciej Sobczak
  2009-12-06 15:59 ` Stephen Leake
  0 siblings, 2 replies; 8+ messages in thread
From: Tomek Walkuski @ 2009-12-05 22:41 UTC (permalink / raw)


Hi,

when interacting with MySQL database I should invoke mysql_library_init
() function first. GNADE does not have it implemented so I tried to do
this on my own:

      function mysql_library_init (
         argc     : int;
         argv     : chars_ptr;
         groups   : chars_ptr
      ) return int;
      pragma Import (C, mysql_library_init, "mysql_library_init");

used as:
     ... := mysql_library_init (0, Null_Ptr, Null_Ptr);

I build my program with -I/usr/include/mysql -L/usr/lib64/mysql -
lmysqlcient and get:
     undefined reference to `mysql_library_init'
     collect2: ld returned 1 exit status
     gnatlink: error when calling /usr/bin/gcc
     gnatmake: *** link failed.

I tested my build setting with mysql_init() and mysql_close() and it
is fine, I cannot get mysql_library_init() and mysql_library_end
working.

Simple C application with these functions works.

Any ideas?




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada and MySQL (mysql_library_init)
  2009-12-05 22:41 Ada and MySQL (mysql_library_init) Tomek Walkuski
@ 2009-12-05 23:03 ` Maciej Sobczak
  2009-12-06 13:06   ` Tomek Walkuski
  2009-12-06 15:59 ` Stephen Leake
  1 sibling, 1 reply; 8+ messages in thread
From: Maciej Sobczak @ 2009-12-05 23:03 UTC (permalink / raw)


On 5 Gru, 23:41, Tomek Walkuski <tomek.walku...@gmail.com> wrote:

> when interacting with MySQL database I should invoke mysql_library_init
> () function first. GNADE does not have it implemented

> Any ideas?

According to MySQL docs, you do not have to call this function,
because mysql_init does it implicitly - you have to just ensure that
you do not call it from multiple threads, which seems straightforward
as it is an initialization function.

You might also want to have a look at the SOCI-Ada library:

http://www.inspirel.com/soci-ada

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada and MySQL (mysql_library_init)
  2009-12-05 23:03 ` Maciej Sobczak
@ 2009-12-06 13:06   ` Tomek Walkuski
  2009-12-06 20:49     ` Maciej Sobczak
  0 siblings, 1 reply; 8+ messages in thread
From: Tomek Walkuski @ 2009-12-06 13:06 UTC (permalink / raw)


On 6 Gru, 00:03, Maciej Sobczak <see.my.homep...@gmail.com> wrote:
> According to MySQL docs, you do not have to call this function,
> because mysql_init does it implicitly - you have to just ensure that
> you do not call it from multiple threads, which seems straightforward
> as it is an initialization function.
>
MySQL docs say:

"In a nonmulti-threaded environment, the call to mysql_library_init()
may be omitted, because mysql_init()  will invoke it automatically as
necessary. However, mysql_library_init() is not thread-safe in a multi-
threaded environment, and thus neither is mysql_init(), which calls
mysql_library_init(). You must either call mysql_library_init()  prior
to spawning any threads, or else use a mutex to protect the call,
whether you invoke mysql_library_init() or indirectly via mysql_init
(). This should be done prior to any other client library call."

So in multi-threaded environment mysql_library_init CANNOT be ommited.

And how about mysql_library_end() ? I could not import it to Ada and
this function performs some memory clean up after disconnection.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada and MySQL (mysql_library_init)
  2009-12-05 22:41 Ada and MySQL (mysql_library_init) Tomek Walkuski
  2009-12-05 23:03 ` Maciej Sobczak
@ 2009-12-06 15:59 ` Stephen Leake
  2009-12-06 16:52   ` Tomek Walkuski
  1 sibling, 1 reply; 8+ messages in thread
From: Stephen Leake @ 2009-12-06 15:59 UTC (permalink / raw)


Tomek Walkuski <tomek.walkuski@gmail.com> writes:

> Hi,
>
> when interacting with MySQL database I should invoke mysql_library_init
> () function first. GNADE does not have it implemented so I tried to do
> this on my own:
>
>       function mysql_library_init (
>          argc     : int;
>          argv     : chars_ptr;
>          groups   : chars_ptr
>       ) return int;
>       pragma Import (C, mysql_library_init, "mysql_library_init");
>
> used as:
>      ... := mysql_library_init (0, Null_Ptr, Null_Ptr);
>
> I build my program with -I/usr/include/mysql -L/usr/lib64/mysql -
> lmysqlcient and get:
>      undefined reference to `mysql_library_init'
>      collect2: ld returned 1 exit status
>      gnatlink: error when calling /usr/bin/gcc
>      gnatmake: *** link failed.
>
> I tested my build setting with mysql_init() and mysql_close() and it
> is fine, I cannot get mysql_library_init() and mysql_library_end
> working.
>
> Simple C application with these functions works.
>
> Any ideas?

Are you building GNADE from scratch? It contains many imports similar
to yours, so it's hard to see why yours would fail but those succeed.

On the other hand, I recently updated the Debian package for GNADE,
and discovered that the MySQL code is severly broken; the Ada types
that are supposed to duplicate C types don't; the C types have
changed.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada and MySQL (mysql_library_init)
  2009-12-06 15:59 ` Stephen Leake
@ 2009-12-06 16:52   ` Tomek Walkuski
  2009-12-07  6:55     ` Stephen Leake
  0 siblings, 1 reply; 8+ messages in thread
From: Tomek Walkuski @ 2009-12-06 16:52 UTC (permalink / raw)


On 6 Gru, 16:59, Stephen Leake <stephen_le...@stephe-leake.org> wrote:
> Are you building GNADE from scratch? It contains many imports similar
> to yours, so it's hard to see why yours would fail but those succeed.
>
I have prepared small program only for testing purposes. I have tried
to import only these two functions.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada and MySQL (mysql_library_init)
  2009-12-06 13:06   ` Tomek Walkuski
@ 2009-12-06 20:49     ` Maciej Sobczak
  2009-12-06 21:13       ` Tomek Walkuski
  0 siblings, 1 reply; 8+ messages in thread
From: Maciej Sobczak @ 2009-12-06 20:49 UTC (permalink / raw)


On 6 Gru, 14:06, Tomek Walkuski <tomek.walku...@gmail.com> wrote:

> MySQL docs say:
>
> "In a nonmulti-threaded environment, the call to mysql_library_init()
> may be omitted, because mysql_init()  will invoke it automatically as
> necessary. However, mysql_library_init() is not thread-safe in a multi-
> threaded environment, and thus neither is mysql_init(), which calls
> mysql_library_init(). You must either call mysql_library_init()  prior
> to spawning any threads, or else use a mutex to protect the call,
> whether you invoke mysql_library_init() or indirectly via mysql_init
> (). This should be done prior to any other client library call."
>
> So in multi-threaded environment mysql_library_init CANNOT be ommited.

Of course it can, and even the docs that you cite above tells you
under what conditions it can be done.
If you plan to use *that* function from multiple threads, you have to
protect it with a mutex. It is dead obvious that you might also ensure
by other means that there is only one thread that calls this function
and if you do ensure that, everything is OK - even if later on you
will have many threads creating the actual connections and interacting
with the database.

Don't be mislead by this "multi-threaded environment" horror. Every
single program that is in principle "multi-threaded" has or can be
restructured to have some initialization phase when it is purely
single-threaded and this is a perfect place to put initialization
calls that are not themselves thread-safe.

Coming back to MySQL and database clients in general - I would be
surprised to see a program that lazily "initializes" the database
environment in the context that is already multi-threaded. This part
of documentation probably reflects the most frequent use-case for
MySQL, which is in web applications implemented in terms of reactive
"scripts" (think about PHP): indeed, there is no explicit
initialization phase and potentially any action might trigger the
initialization calls.
But if you write in Ada you can do better (even with AWS).

> And how about mysql_library_end() ? I could not import it to Ada and
> this function performs some memory clean up after disconnection.

I think that the world will not end tomorrow if you "forget" to call
it.
Think about programs that never finish and/or that use the database
all the time - these programs, well, don't call it.

And really - what is exactly your problem with importing these two
functions?
Let me guess - they are *macros*. What a mess...

(checking the mysql.h header ... yes, they are macros)

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com

Database Access Library for Ada: www.inspirel.com/soci-ada



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada and MySQL (mysql_library_init)
  2009-12-06 20:49     ` Maciej Sobczak
@ 2009-12-06 21:13       ` Tomek Walkuski
  0 siblings, 0 replies; 8+ messages in thread
From: Tomek Walkuski @ 2009-12-06 21:13 UTC (permalink / raw)


On 6 Gru, 21:49, Maciej Sobczak <see.my.homep...@gmail.com> wrote:
> And really - what is exactly your problem with importing these two
> functions?
> Let me guess - they are *macros*. What a mess...
>
> (checking the mysql.h header ... yes, they are macros)
>

Thank you for your answer.

My question was not about: is it good or bad to call these functions?
I wanted to know why I can't import them.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada and MySQL (mysql_library_init)
  2009-12-06 16:52   ` Tomek Walkuski
@ 2009-12-07  6:55     ` Stephen Leake
  0 siblings, 0 replies; 8+ messages in thread
From: Stephen Leake @ 2009-12-07  6:55 UTC (permalink / raw)


Tomek Walkuski <tomek.walkuski@gmail.com> writes:

> On 6 Gru, 16:59, Stephen Leake <stephen_le...@stephe-leake.org> wrote:
>> Are you building GNADE from scratch? It contains many imports similar
>> to yours, so it's hard to see why yours would fail but those succeed.
>>
> I have prepared small program only for testing purposes. I have tried
> to import only these two functions.

Ok.

A good alternative is to use the GNADE ODBC interface; there are ODBC
interfaces to MySQL on both Linux and Windows.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2009-12-07  6:55 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-12-05 22:41 Ada and MySQL (mysql_library_init) Tomek Walkuski
2009-12-05 23:03 ` Maciej Sobczak
2009-12-06 13:06   ` Tomek Walkuski
2009-12-06 20:49     ` Maciej Sobczak
2009-12-06 21:13       ` Tomek Walkuski
2009-12-06 15:59 ` Stephen Leake
2009-12-06 16:52   ` Tomek Walkuski
2009-12-07  6:55     ` Stephen Leake

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox