An example with MySQL/MariaDB

Please follow the instructions explained in the section called “MySQL Configuration” to set-up a running environment for MySQL or MariaDB server.

Start the LIXA state server

Start the state server as shown below:

[Shell terminal session]
tiian@ubuntu1204-64:/tmp$ sudo su - lixa
lixa@ubuntu1204-64:~$ /opt/lixa/sbin/lixad --daemon
lixa@ubuntu1204-64:~$ exit
logout
tiian@ubuntu1204-64:/tmp$ ps -ef|grep lixad|grep -v grep
lixa      1804     1  0 22:45 ?        00:00:00 /opt/lixa/sbin/lixad --daemon
	  

Build the client program

Prepare the client (Application Program) using the below commands (gcc command was splitted on several lines using \ to help readability, but you may use a single line):

[Shell terminal session]
tiian@ubuntu1204-64:/tmp$ mkdir tmp
tiian@ubuntu1204-64:/tmp$ cd tmp
tiian@ubuntu1204-64:/tmp/tmp$ cp /opt/lixa/share/doc/lixa-1.3.2-dev/examples/example8_mys.c .
tiian@ubuntu1204-64:/tmp/tmp$ gcc example8_mys.c $(/opt/lixa/bin/lixa-config -c -f -m -d) \
> $(mysql_config --include --libs_r) -o example8_mys
	  

Verify the executable produced by gcc:

[Shell terminal session]
tiian@ubuntu1204-64:/tmp/tmp$ ldd example8_mys
        linux-vdso.so.1 =>  (0x00007fff37367000)
	liblixac.so.0 => /opt/lixa/lib/liblixac.so.0 (0x00007fd64e935000)
	liblixamy.so.0 => /opt/lixa/lib/liblixamy.so.0 (0x00007fd64e72d000)
	libmysqlclient.so.18 => /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18 (0x00007fd64e1db000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd64de1d000)
	libgmodule-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgmodule-2.0.so.0 (0x00007fd64dc19000)
	libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007fd64d923000)
	libxml2.so.2 => /usr/lib/x86_64-linux-gnu/libxml2.so.2 (0x00007fd64d5c7000)
	liblixab.so.0 => /opt/lixa/lib/liblixab.so.0 (0x00007fd64d3ad000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fd64d18f000)
	libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fd64cf78000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fd64cd74000)
	librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fd64cb6b000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fd64c86f000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fd64eb54000)
	libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007fd64c632000)
	libuuid.so.1 => /lib/x86_64-linux-gnu/libuuid.so.1 (0x00007fd64c42c000)
	  

Set-up LIXA environment

Set-up the LIXA_PROFILE environment variable:

[Shell terminal session]
tiian@ubuntu1204-64:/tmp/tmp$ echo $LIXA_PROFILE

tiian@ubuntu1204-64:/tmp/tmp$ export LIXA_PROFILE=MYS_STA
tiian@ubuntu1204-64:/tmp/tmp$ echo $LIXA_PROFILE
MYS_STA
	  

Some checks before program execution

We set LIXA_PROFILE to value MYS_STA, looking at /opt/lixa/etc/lixac_conf.xml:

    <profile name="MYS_STA">
      <sttsrvs>
        <sttsrv>local_1</sttsrv>
      </sttsrvs>
      <rsrmgrs>
        <rsrmgr>MySQL_stareg</rsrmgr>
      </rsrmgrs>
    </profile>
	

the profile references the Resource Manager named MySQL_stareg, looking again at the config file:

    <rsrmgr name="MySQL_stareg" switch_file="/opt/lixa/lib/switch_mysql_stareg.so" xa_open_info="host=localhost,user=lixa,passwd=,db=lixa,client_flag=0" xa_close_info="" />
	

we can discover how LIXA is configured to access the MySQL database. The content of xa_open_info is passed to mysql_real_connect function after some parsing has occurred. This is the prototype of the mysql_real_connect function as described in the official manual

MYSQL *mysql_real_connect(const char *host, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag);

and these are the tokens accepted by LIXA inside xa_open_info:

  • host: a string of characters

  • user: a string of characters

  • passwd: a string of characters

  • db: a string of characters

  • port: a number

  • unix_socket: a string of characters

  • client_flag: a number

As shown in the above example, this is the syntax that applies to xa_open_info:

  1. an element is composed by <token>=<value> where <token> is one of the keywords listed above

  2. a comma , separates two elements

  3. <value> may be empty (like passwd in the above example): it will passed as an empty string ("") to mysql_real_connect

  4. if a <token> is not specified in xa_open_info, LIXA will pass to mysql_real_connect a NULL pointer if the missing token refers to a character string and 0 value if the missing token refers to a number

  5. if you must put an equal symbol ("=") inside <value> you must put two instead of one: "=="

  6. if you must put a comma symbol (",") inside <value> you must put two instead of one: ",,"

  7. all control characters like space, tab, newline and so on, are passed as is without any modification

Using this configuration: xa_open_info="host=localhost,user=lixa,passwd=,db=lixa,client_flag=0" LIXA will call mysql_real_connect as

mysql_real_connect(conn, "localhost", "lixa", "", "lixa", 0, NULL, 0)

You should refer to MySQL official documentation to discover how you can configure xa_open_info.

Warning

xa_open_info can contain a maximum of 255 characters (plus \0 string terminator); if you need more space, consider to move some parameters to mycnf file (see MySQL official documentation to pick-up the details).

Program execution

It is suggested to open two different terminals: the first one connected to lixa MySQL database and the second one pointing to the directory where the compiled program example8_mys lives. First teminal session:

[MySQL terminal session]
tiian@ubuntu1204-64:/tmp/tmp$ mysql -h localhost -u lixa lixa
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 5.5.54-0ubuntu0.12.04.1 (Ubuntu)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
	  

Second teminal session:

[Shell terminal session]
tiian@ubuntu1204-64:/tmp/tmp$ ls -la
total 28
drwxrwxr-x 2 tiian tiian  4096 mar  3 22:47 .
drwxrwxrwt 5 root  root   4096 mar  3 22:47 ..
-rwxrwxr-x 1 tiian tiian 13052 mar  3 22:47 example8_mys
-rw-r--r-- 1 tiian tiian  3374 mar  3 22:46 example8_mys.c
	  

Check the content of authors table before program execution:

[MySQL terminal session]
mysql> SELECT * FROM authors;
Empty set (0.04 sec)
	  

Execute the program:

[Shell terminal session]
tiian@ubuntu1204-64:/tmp/tmp$ ./example8_mys insert
Inserting a row in the table...
	  

Check the content of the table again:

[MySQL terminal session]
mysql> SELECT * FROM authors;
+----+-----------+------------+
| id | last_name | first_name |
+----+-----------+------------+
|  1 | Foo       | Bar        |
+----+-----------+------------+
1 row in set (0.00 sec)
	  

The example program inserted the row with id=1. You can not insert the same row twice because there is a unique constraint on this table, but you can remove the row using

[Shell terminal session]
tiian@ubuntu1204-64:/tmp/tmp$ ./example8_mys delete
Deleting a row from the table...
	  

Check the table content again:

[MySQL terminal session]
mysql> SELECT * FROM authors;
Empty set (0.00 sec)
	  

you can verify in file /opt/lixa/etc/lixac_conf.xml that MYS_STA is associated to static registration [30] . Execute the program:

[Shell terminal session]
tiian@ubuntu1204-64:/tmp/tmp$ export LIXA_TRACE_MASK=0x00002000
tiian@ubuntu1204-64:/tmp/tmp$ echo $LIXA_TRACE_MASK
0x00002000
tiian@ubuntu1204-64:/tmp/tmp$ ./example8_mys insert 2>&1 | grep xa_start
2017-03-03 22:51:51.988732 [2030/140079512323904] lixa_xa_start
2017-03-03 22:51:51.988757 [2030/140079512323904] lixa_xa_start: sending 213 bytes to the server for step 8
2017-03-03 22:51:52.044451 [2030/140079512323904] lixa_xa_start: receiving 95 bytes from the server |<?xml version="1.0" encoding="UTF-8" ?><msg level="0" verb="3" step="16"><answer rc="0"/></msg>|
2017-03-03 22:51:52.044627 [2030/140079512323904] lixa_xa_start: xa_start_entry(xid, 0, 0x0) = 0
2017-03-03 22:51:52.044639 [2030/140079512323904] lixa_xa_start: sending 210 bytes to the server for step 24
2017-03-03 22:51:52.044713 [2030/140079512323904] lixa_xa_start/excp=10/ret_cod=0/errno=0
	  

Finally, clean up the table again:

[Shell terminal session]
tiian@ubuntu1204-64:/tmp/tmp$ unset LIXA_TRACE_MASK
tiian@ubuntu1204-64:/tmp/tmp$ ./example8_mys delete
Deleting a row from the table...
	  



[30] The current implementation does not support dynamic registration for MySQL.