Re: Is there a real java source sample for MySQL Connector/MXJ?

Eric Herman <[email protected]> Tue, 09 Dec 2008 11:40:52 +0100
Newsgroups gmane.comp.db.mysql.java
Message-ID <[email protected]>
Hello Ben,

Thank you for your interest.

Ben Stover wrote:
> When I go to page:
> 
> http://dev.mysql.com/doc/refman/5.0/en/connector-mxj.html
> 
> I found a lot of information about on how to install & configure
> MySQL connector/MXJ but NOT on how use it from inside a java app.


Included in the src/ are two sample usages:

  src/ConnectorMXJUrlTestExample.java

and:

  src/ConnectorMXJObjectTestExample.java

> 
> As far as I understood I can manage (=e.g. shutdown + start) a MySQL
> database.

That is correct, and this is what the examples demonstrate.

> So how would these lines of code for this purpose look like in java
> in detail??
> 

Both of the included examples I mentioned have the same idea of
deploying a new mysqld installation, starting mysqld, issuing a query,
and finally shutting mysqld down.


By looking at the URL Example, you can see that an application would
have to make a couple of changes, which will look a little like this:

(0) Add the MySQL Connecctor/MXJ classes to the $CLASSPATH

(1) Alter your jdbc connection string from somthing like this:

       String url = "jdbc:mysql://localhost:3306/somedb"
                  + "?createDatabaseIfNotExist=true";

   To something more like this:

       String url = "jdbc:mysql:mxj://localhost:13306/somedb"
                  + "?createDatabaseIfNotExist=true"
                  + "&server.basedir=/path/to/deploy/mysql";


   The first connection to the database will deploy and start mysqld.
Additional connections will recognize that a database already is running.

(2) Finally, when it is time to shutdown the database,

       File dbDir = new File("/path/to/deploy/mysql");
       File dataDir = null; /* defaults to dbDir's "data" subdir */

       ServerLauncherSocketFactory.shutdown(dbDir, dataDir);


Notice two key things about the URL above:
(A) the "mxj:" which tells Connector/J to use Connector/MXJ and
(B) the "server." prefix to an parameter. All "server." parameters will
be used by Connector/MXJ. In this way, anything that can normally be
specified to configure a mysqld can be configured via the URL with this
prefix.

For instance, your app could have a very detailed configuration, with
many Connector/J parameters as well as many mysqld parameters:

     String url = "jdbc:mysql:mxj://127.0.0.1:13306/dbName"
             + "?createDatabaseIfNotExist=true"
             + "&elideSetAutoCommits=true"
             + "&useServerPrepStmts=false"
             + "&cachePrepStmts=true"
             + "&cacheCallableStmts=true"
             + "&noAccessToProcedureBodies=true"
             + "&characterEncoding=UTF-8"
             + "&includeInnodbStatusInDeadlockExceptions=true"
             + "&cacheServerConfiguration=true"
             + "&useDynamicCharsetInfo=false"
             + "&server.initialize-user=true"
             + "&server.basedir=/path/to/deploy/mysql"
             + "&server.datadir=/path/to/var/mysql/data"
             + "&server.innodb_locks_unsafe_for_binlog=1"
             + "&server.innodb_buffer_pool_size=128M"
             + "&server.innodb_log_file_size=32M"
             + "&server.innodb_flush_log_at_trx_commit=2"
             + "&server.innodb_support_xa=0"
             + "&server.skip-locking"
             + "&server.key_buffer=32M"
             + "&server.max_allowed_packet=64M"
             + "&server.table_cache=64"
             + "&server.sort_buffer_size=512K"
             + "&server.net_buffer_length=16K"
             + "&server.read_buffer_size=256K"
             + "&server.read_rnd_buffer_size=512K"
             + "&server.myisam_sort_buffer_size=16M";

As you can imagine, there is quite a bit of tuning available, if needed.


As an alternative to the URL syntax, the Object Test Example shows how
an instance of the MysqldResource may be instantiated directly as a POJO
(Plain Old Java Object):

       MysqldResource mysqldResource = new MysqldResource(databaseDir);
       Map databaseOptions = new HashMap();
       databaseOptions.put("port", "13306");
       mysqldResource.start("thread-name", databaseOptions)

And finally:

       mysqldResource.shutdown();


The databaseOptions map may contain any option that mysqld can be
configured with, just like the urls above. For instance:
       databaseOptions.put("max_allowed_packet", "64M");

> Do I really need an AppServer for that?

No. As you can see, Connector/MXJ may be invoked via JDBC connection
parameters, and directly as a POJO, in addition to being instantiated as
a JMX MBean.

If Connector/MXJ might be appropriate for your application, then please
download the package, review the sources, and give one of the samples a
try.

Cheers,
  -Eric

> 
> Ben
> 
> 
> 
> 


-- 
Eric Herman, Software Developer
Sun MySQL www.mysql.com
Mobile: +31 62 071 9662

-- 
MySQL Java Mailing List
For list archives: http://lists.mysql.com/java
To unsubscribe:    http://lists.mysql.com/[email protected]