<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" >

<channel><title><![CDATA[A Web Developer's Diary - Blog]]></title><link><![CDATA[http://www.webdevelopersdiary.com/blog]]></link><description><![CDATA[Blog]]></description><pubDate>Wed, 05 Apr 2023 17:10:50 -0700</pubDate><generator>Weebly</generator><item><title><![CDATA[Play 2 Framework on Maven with auto-reloading through IDE]]></title><link><![CDATA[http://www.webdevelopersdiary.com/blog/play-2-framework-on-maven-with-auto-reloading-through-ide]]></link><comments><![CDATA[http://www.webdevelopersdiary.com/blog/play-2-framework-on-maven-with-auto-reloading-through-ide#comments]]></comments><pubDate>Mon, 27 Apr 2015 12:17:36 GMT</pubDate><category><![CDATA[java]]></category><category><![CDATA[maven]]></category><category><![CDATA[play-framework]]></category><category><![CDATA[scala]]></category><guid isPermaLink="false">http://www.webdevelopersdiary.com/blog/play-2-framework-on-maven-with-auto-reloading-through-ide</guid><description><![CDATA[Are you looking for a way to run Play 2 Framework on Maven with its auto-reloading functionality? You've come to the right place, read on.I've been working with Play2 Framework for several years now and it's a very nice way to develop in Scala. Some of the Play projects have grown rather large and have become sluggish in combination with SBT. Resolving dependencies, compiling and starting Play all take a long time with SBT. Perhaps my biggest objection is that SBT can not stage a self-contained  [...] ]]></description><content:encoded><![CDATA[<div class="paragraph" style="text-align:left;">Are you looking for a way to run Play 2 Framework on Maven with its auto-reloading functionality? You've come to the right place, read on.<br><br>I've been working with Play2 Framework for several years now and it's a very nice way to develop in Scala. Some of the Play projects have grown rather large and have become sluggish in combination with SBT. Resolving dependencies, compiling and starting Play all take a long time with SBT. Perhaps my biggest objection is that SBT can not stage a self-contained integration-test environment. These shortcomings have forced me to look at SBT alternatives. I arrived at a way to build and run Play projects with support for auto-reloading using Maven, play2-maven-plugin, sbt-compiler-maven-plugin, Eclipse, DCEVM and Zinc. This article describes how to set up such an environment for your own enjoyment.<br><br>Start by downloading the following tools:<br><ul><li><a href="http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.html#jdk-7u51-oth-JPR" target="_blank" title="">Java SE Development Kit 7u51</a>&nbsp;- needs registration at Oracle (which is unfortunate but free)</li><li><a href="https://github.com/dcevm/dcevm/releases/download/full-7u51%2B3/installer-full-jdk7u51.3.jar" title="">DCEVM (full) for Java 7u51 (build 3)</a>&nbsp;- make sure to get the full version and not the light version</li><li><a href="http://scala-ide.org/download/sdk.html" target="_blank" title="">Scala IDE for Eclipse</a>&nbsp;- I used version Scala IDE 4.0.0 and Eclipse 4.4 Luna</li><li><a href="https://github.com/typesafehub/zinc" target="_blank" title="">Zinc</a>&nbsp;latest version - I used <a href="http://downloads.typesafe.com/zinc/0.3.7/zinc-0.3.7.tgz" title="">Zinc 0.3.7</a></li><li><a href="https://maven.apache.org/download.cgi" target="_blank" title="">Apache Maven</a>&nbsp;latest version - I used Maven 3.2.3</li></ul><br>Which version of Scala IDE, Eclipse, Zinc or Maven you install should not matter much. However, the Java and <a href="https://github.com/dcevm/dcevm/releases" target="_blank" title="">DCEVM versions</a> do matter. It is paramount that the build versions of both Java and DCEVM (in this case 7u51) match perfectly and that you get a full version of DCEVM.<br></div><div class="paragraph" style="text-align:left;">Next we are going to patch Java with DCEVM full version. Run the DCEVM installer as root after downloading it by executing the following command from a terminal:</div><div><div id="579509936698267798" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><code>$ sudo java -jar installer-full-jdk7u51.3.jar</code></div></div><div class="paragraph" style="text-align:left;">This will open a GUI which asks you to specify the Java home directory for Java 7u51. If it's not already there, click the "Add installation directory..." and browse to the right Java home directory (look this up for your own operating system, this may be different). Then select the right Directory for Java version 1.7.0_51 and click "Install DCEVM as altjvm".</div><div><div class="wsite-image wsite-image-border-thin" style="padding-top:10px;padding-bottom:10px;margin-left:0;margin-right:0;text-align:center"><a><img src="http://www.webdevelopersdiary.com/uploads/1/2/7/7/12771307/718408_orig.png" alt="Picture" style="width:auto;max-width:100%"></a><div style="display:block;font-size:90%"></div></div></div><div class="paragraph" style="text-align:justify;">Apache Maven, Scala IDE and Zinc are all available to download as binaries. Download and extract the binary the respective binary archives and you should be good to go. <strong>Special note on running Maven</strong>: you have to set the <strong>JAVA_HOME</strong> environment variable. This is explained on the <a href="https://maven.apache.org/download.cgi" target="_blank" title="">Maven download page</a> (scroll down) for each operating system. The JAVA_HOME environment variable should be set to the same path you selected in the DCEVM GUI.<br><br><span style="">Make sure that Java 7u51 is your default Java version. You can verify this by running `java -version` from the command line. Also make sure DCEVM is correctly patched into the JVM as an alternative JVM, which can be verified by running `java -XXaltjvm=dcevm -version`, which should show something like:</span><br></div><div><div id="916967444529571424" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><code>$ java -version<br>java version "1.7.0_51"<br>Java(TM) SE Runtime Environment (build 1.7.0_51-b13)<br>Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)<br>$ java -XXaltjvm=dcevm -version<br>java version "1.7.0_51"<br>Java(TM) SE Runtime Environment (build 1.7.0_51-b13)<br>Dynamic Code Evolution 64-Bit Server VM (build 24.51-b03-dcevmfull-3, mixed mode)</code></div></div><div class="paragraph" style="text-align:left;">Start Zinc by going to the extracted Zinc directory and running:</div><div><div id="474653153169925013" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><code>$ ./bin/zinc -start<br>Nailgun server running with 0 cached compilers<br><br>Version = 0.3.7<br><br>Zinc compiler cache limit = 5<br>Resident scalac cache limit = 0<br>Analysis cache limit = 5</code></div></div><div class="paragraph" style="text-align:left;">Start up the Scala IDE (run the file called `eclipse` in the extracted directory) and import this <a href="https://github.com/webdevelopersdiary/play2-maven-examples/tree/master/play2-maven-hotreload" target="_blank" title="">example Play2 Maven Hotreload</a> project into Eclipse. Then enable the "eclipse" build profile by right-clicking on the "play2-maven-hotreload" project and go to Maven &gt; Select Maven Profiles. Make sure only "eclipse" is checked and click OK. Right click the project again and go to Configure &gt; Add Scala nature. The eclipse project is now set up correctly.</div><div class="paragraph" style="text-align:left;"><strong>Note</strong>: at the time of writing the current play2-maven-plugin release does not have Dev mode support yet and as a result the static resources are cached by the browser in Prod mode. Grab this modified version of the play2-maven-plugin to build a snapshot that supports Play in Dev mode.</div><div><div id="915120413121477616" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><code>$ git clone https://github.com/webdevelopersdiary/play2-maven-plugin<br>$ cd play2-maven-plugin/plugin<br>$ mvn compile install</code></div></div><div class="paragraph" style="text-align:left;">Go back to the play2-maven-hotreload directory on the command line and run:</div><div><div id="227398866711128334" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><code>$ mvn -P hotreload compile play2:run</code></div></div><div class="paragraph" style="text-align:left;">Wait until you see that Play has started (in Dev mode) and then start a remote debugging session in Eclipse with the following debug configuration:</div><div><div class="wsite-image wsite-image-border-thin" style="padding-top:10px;padding-bottom:10px;margin-left:0;margin-right:0;text-align:center"><a><img src="http://www.webdevelopersdiary.com/uploads/1/2/7/7/12771307/9913073_orig.png" alt="Picture" style="width:auto;max-width:100%"></a><div style="display:block;font-size:90%"></div></div></div><div class="paragraph" style="text-align:left;">Go to <a href="http://localhost:9000" target="_blank" title="">http://localhost:9000</a> and take a look at the page. Now edit app/controllers/Application.scala or public/javascripts/hello.js and wait for Eclipse to finish compiling. The first compile attempt might take a few seconds, subsequent compile runs should be within one or two seconds thanks to Zinc. Refresh the page. If all went alright, the changes should be visible without restarting! As an added benefit, the Play server was not reloaded internally like in SBT. This gives you an extra speed boost, which is significant for large Play apps that rely on multiple database connections and cache servers, for example.<br><br>I imagine this will also work with other IDEs that have hotswapping support, like Netbrains IntelliJ IDEA, but I have not tried others so far. If you have tried, please drop your experience in the comments below. Grzegorz Slowikowski, the author of <a href="http://code.google.com/p/play2-maven-plugin/" target="_blank">play2-maven-plugin</a>, has brought to my attention that LinkedIn is planning to bring <a href="http://engineering.linkedin.com/play/developing-play-applications-using-gradle" target="_blank" title="">hot-reload support for Play 2 to Gradle</a>. Somewhere in the future the play2-maven-plugin will support hot-reloading natively as well, until that time it will remain in beta. Simpler and more portable alternatives to SBT are on the way, stay tuned. Thanks for reading.</div>]]></content:encoded></item><item><title><![CDATA[OpenCL on Ubuntu 12.10 with Intel CPU]]></title><link><![CDATA[http://www.webdevelopersdiary.com/blog/opencl-on-ubuntu-1210-with-intel-cpu]]></link><comments><![CDATA[http://www.webdevelopersdiary.com/blog/opencl-on-ubuntu-1210-with-intel-cpu#comments]]></comments><pubDate>Sat, 04 May 2013 07:02:23 GMT</pubDate><category><![CDATA[opencl]]></category><category><![CDATA[ubuntu]]></category><guid isPermaLink="false">http://www.webdevelopersdiary.com/blog/opencl-on-ubuntu-1210-with-intel-cpu</guid><description><![CDATA[ If you are trying to get some test scripts running on Ubuntu 12.10 and you can't get the Intel SDK to work, here's a handy tip. Go over to the AMD website and download their AMD APP SDK. As long as your CPU supports SSE, the AMD SDK will provide you with OpenCL capabilities on any processor.    I was having trouble getting Intel's SDK to work, as well as their open source variant Beignet. Both versions did not reckognize any OpenCL platforms or devices. I am on a Intel Core I5 CPU with Intel HD [...] ]]></description><content:encoded><![CDATA[<div class="paragraph" style="text-align:left;"> If you are trying to get some test scripts running on Ubuntu 12.10 and you can't get the Intel SDK to work, here's a handy tip. Go over to the AMD website and <a href="http://developer.amd.com/tools-and-sdks/heterogeneous-computing/amd-accelerated-parallel-processing-app-sdk/downloads/" target="_blank">download their AMD APP SDK</a>. As long as your CPU supports SSE, the AMD SDK will provide you with OpenCL capabilities on any processor. </div>  <div class="paragraph" style="text-align:left;"> I was having trouble getting <a href="http://software.intel.com/en-us/vcsource/tools/opencl-sdk-xe" target="_blank">Intel's SDK</a> to work, as well as their open source variant <a href="http://cgit.freedesktop.org/beignet/tree/README.md" target="_blank">Beignet</a>. Both versions did not reckognize any OpenCL platforms or devices. I am on a Intel Core I5 CPU with Intel HD Graphics 4000 (it's a shame I can't use that on Ubuntu yet, though I am happy to be running sample code now :-)). </div>  <div class="paragraph" style="text-align:left;"> For your convenience, here are the steps to download and install OpenCL on Ubuntu:<br> <br> First, download the SDK. At the time of writing, the newest version is version 2.8.1.0. For 32 bit platforms:<br> </div>  <div> <div id="999730587477000789" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"> <a href="http://developer.amd.com/wordpress/media/files/AMD-APP-SDK-v2.8.1.0-lnx32.tgz" target="_blank">http://developer.amd.com/wordpress/media/files/AMD-APP-SDK-v2.8.1.0-lnx32.tgz</a> </div> </div>  <div class="paragraph" style="text-align:left;"> Or for 64<span style="font-size: 1em; line-height: 1.5;">&nbsp;bit platforms:</span> </div>  <div> <div id="646515222303310554" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"> <a href="http://developer.amd.com/wordpress/media/files/AMD-APP-SDK-v2.8.1.0-lnx64.tgz" target="_blank">http://developer.amd.com/wordpress/media/files/AMD-APP-SDK-v2.8.1.0-lnx64.tgz</a> </div> </div>  <div class="paragraph" style="text-align:left;"> Extract the file you just downloaded and run the installer: </div>  <div> <div id="306401877351533182" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"> <code>$ tar -xzvf AMD-APP-SDK-v2.8*-lnx*.tgz<br> $ sudo ./Install-AMD-APP.sh</code> </div> </div>  <div class="paragraph" style="text-align:left;"> As a final step reboot your computer and then you should be able to run OpenCL applications on Ubuntu. </div>  <div class="paragraph" style="text-align:left;"> <strong>Update August 19th, 2013:</strong> updated AMD APP SDK download links from version 2.8 to version 2.8.1.0 and updated recommended download method, thanks to Jacek for pointing that out! </div> ]]></content:encoded></item><item><title><![CDATA[How to stop Magento's emails from bouncing]]></title><link><![CDATA[http://www.webdevelopersdiary.com/blog/how-to-stop-magentos-emails-from-bouncing]]></link><comments><![CDATA[http://www.webdevelopersdiary.com/blog/how-to-stop-magentos-emails-from-bouncing#comments]]></comments><pubDate>Mon, 21 Jan 2013 09:46:23 GMT</pubDate><category><![CDATA[magento]]></category><guid isPermaLink="false">http://www.webdevelopersdiary.com/blog/how-to-stop-magentos-emails-from-bouncing</guid><description><![CDATA[Recently I noticed some emails sent by Magento keep bouncing back. The mail server dropped me a few "Undelivered Mail Returned to Sender" messages. This is very&nbsp;undesirable, since this means that some order confirmations and/or invoices are not being delivered to the customer. After some investigation I discovered a Magento setting under&nbsp;System &gt; Configuration &gt; Advanced &gt; System &gt; Mail Sending Settings &gt; "Set Return-Path". Set this value to "Yes" to make the Return-Path [...] ]]></description><content:encoded><![CDATA[<div class="paragraph" style="text-align:left;">Recently I noticed some emails sent by Magento keep bouncing back. The mail server dropped me a few "Undelivered Mail Returned to Sender" messages. This is very&nbsp;undesirable, since this means that some order confirmations and/or invoices are not being delivered to the customer. After some investigation I discovered a Magento setting under&nbsp;System &gt; Configuration &gt; Advanced &gt; System &gt; Mail Sending Settings &gt; "Set Return-Path". Set this value to "Yes" to make the Return-Path the same as the sender. Alternatively, you can set it to "Specified" and specify a "Return-Path Email" where all bounced emails get sent to. This can come in handy if you want to have those emails checked with priority for example, you could sent bounced mails to a priority mailbox instead.<br /><br />Not specifying a Return-Path in Magento (ie. setting "Set Return-Path" to "No") will most likely get your emails blocked by a lot of spam filters.</div>]]></content:encoded></item><item><title><![CDATA[MySQL database replication using SSL on Ubuntu 12.04]]></title><link><![CDATA[http://www.webdevelopersdiary.com/blog/mysql-database-replication-using-ssl-on-ubuntu-1204]]></link><comments><![CDATA[http://www.webdevelopersdiary.com/blog/mysql-database-replication-using-ssl-on-ubuntu-1204#comments]]></comments><pubDate>Sun, 15 Jul 2012 13:05:56 GMT</pubDate><category><![CDATA[mysql]]></category><category><![CDATA[ubuntu]]></category><guid isPermaLink="false">http://www.webdevelopersdiary.com/blog/mysql-database-replication-using-ssl-on-ubuntu-1204</guid><description><![CDATA[This tutorial describes how to set up database replication for MySQL using SSL on Ubuntu 12.04. There are some tutorials out there, but I found out they are a little outdated and don't work as well any more (at least not for Ubuntu specifically). So I decided to write down my experience for other developers who find them selves setting up MySQL database replication to make their lives easier.First, we start with setting up the master server. Last, we set up the slave server to download database  [...] ]]></description><content:encoded><![CDATA[<div class="paragraph" style="text-align:left;">This tutorial describes how to set up database replication for MySQL using SSL on Ubuntu 12.04. There are some tutorials out there, but I found out they are a little outdated and don't work as well any more (at least not for Ubuntu specifically). So I decided to write down my experience for other developers who find them selves setting up MySQL database replication to make their lives easier.<br /><br />First, we start with setting up the master server. Last, we set up the slave server to download database changes from the master server. For the purpose of this tutorial we are going to replicate a database called 'exampledb' from a server at master.webdevelopersdiary.com to a server at slave.webdevelopersdiary.com.</div>  <h2 style="text-align:left;">Set up the master server</h2>  <div class="paragraph" style="text-align:left;">Setting up the master consists of the following steps:<br /><ol><li>Generate SSL certificates.</li><li>Edit MySQL configuration my.cnf.</li><li>Restart MySQL server process.</li><li>Set up database replication privileges (and force SSL).</li><li>Perform initial database backup to start replication from.</li><li>Finish up.</li></ol></div>  <div class="paragraph" style="text-align:left;">The SSL certificates have to be placed in the /etc/mysql/ directory if you want them to work out-of-the-box. If you want to place them elsewhere, you first have to update the MySQL Apparmor settings, or SSL will fail. For the purpose of this tutorial, we will assume all the certificates will be in /etc/mysql/. The MySQL Manual explains very well how to generate the SSL certificates. I copied the steps here for your convenience. First, we generate the CA certificate:</div>  <div><div id="194811450663686855" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre> $ mkdir ~/mysql-tutorial/ && cd ~/mysql-tutorial/<br /> $ openssl genrsa 2048 > ca-key.pem<br /> $ openssl req -new -x509 -nodes -days 1000 \<br />   -key ca-key.pem -out ca-cert.pem </pre></div>    </div>  <div class="paragraph" style="text-align:left;">Then create the server certificate, remove its passphrase and sign it:<br /></div>  <div><div id="442089840479460446" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre> $ openssl req -newkey rsa:2048 -days 1000 \<br />   -nodes -keyout server-key.pem -out server-req.pem<br /> $ openssl rsa -in server-key.pem -out server-key.pem<br /> $ openssl x509 -req -in server-req.pem -days 1000 \<br />   -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem </pre></div>    </div>  <div class="paragraph" style="text-align:left;">Create the client certificate, remove its passphrase and sign it:<br /></div>  <div><div id="239419298664061739" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre> $ openssl req -newkey rsa:2048 -days 1000 \<br /> &nbsp; -nodes -keyout client-key.pem -out client-req.pem<br /> $ openssl rsa -in client-key.pem -out client-key.pem<br /> $ openssl x509 -req -in client-req.pem -days 1000 \<br /> &nbsp; -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem </pre></div>    </div>  <div class="paragraph" style="text-align:left;">Install the certificates into to /etc/mysql/ directory:</div>  <div><div id="873543553215053851" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre>$ sudo cp *.pem /etc/mysql/</pre></div>    </div>  <div class="paragraph" style="text-align:left;">Open /etc/mysql/my.cnf and make sure MySQL is listening on all interfaces so that the backup server can reach the database from the outside. Typically, there is a line like:<br /></div>  <div><div id="443607192681649526" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code># Instead of skip-networking the default is now to listen only on<br /> # localhost which is more compatible and is not less secure.<br /> #bind-address            = 127.0.0.1</code></pre></div>    </div>  <div class="paragraph" style="text-align:left;">The line starting with &ldquo;bind-address&rdquo; should be commented out if present.<br />Also add the following in the [mysqld] section of the config file:<br /></div>  <div><div id="511366425166004894" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code># The following can be used as easy to replay backup logs or for replication.<br /> # note: if you are setting up a replication slave, see README.Debian about<br /> #       other settings you may need to change.<br /> server-id	 = 1<br /> log_bin	 = /var/log/mysql/mysql-bin.log<br /> expire_logs_days	= 10<br /> max_binlog_size	= 100M<br /> binlog_do_db	= exampledb</code></pre></div>    </div>  <div class="paragraph" style="text-align:left;">And enable SSL by editing:<br /></div>  <div><div id="523391181397547919" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code># For generating SSL certificates I recommend the OpenSSL GUI "tinyca".<br /> ssl-ca=/etc/mysql/ca-cert.pem<br /> ssl-cert=/etc/mysql/server-cert.pem<br /> ssl-key=/etc/mysql/server-key.pem</code></pre></div>    </div>  <div class="paragraph" style="text-align:left;">Close /etc/mysql/my.cnf and restart MySQL to apply changes to the config by executing the command:<br /></div>  <div><div id="616135241496210034" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre>$ sudo service mysql restart</pre></div>    </div>  <div class="paragraph" style="text-align:left;">Next, we create a database dump of the database we want to replicate (exampledb). During the dump, we need to lock the database to determine the exact position from which we need to start replicating (corresponding to the database dump). So we write down the log &lsquo;position&rsquo; column from which to start replicating on the slave.<br /><br />Login to the mysql server (located at master.webdevelopersdiary.com) as root from command line (or use a web-based admin tool like phpMyAdmin to execute the required commands):<br /></div>  <div><div id="843597019423814184" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre>$ mysql -h localhost -u root -p</pre></div>    </div>  <div class="paragraph" style="text-align:left;">Execute the following SQL commands:<br /></div>  <div><div id="452139054800246678" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>GRANT REPLICATION SLAVE ON *.*<br /> TO 'slave_user'@'slave.webdevelopersdiary.com'<br /> IDENTIFIED BY 'slave_password'<br /> REQUIRE SSL;<br /> FLUSH PRIVILEGES;<br /> USE exampledb;<br /> FLUSH TABLES WITH READ LOCK;<br /> SHOW MASTER STATUS;</code></pre></div>    </div>  <div class="paragraph" style="text-align:left;">Write down the output of the last statement, you're going to need it later. An example output looks like:</div>  <div><div id="547395472426005766" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>+------------------+----------+-----------------+------------------+<br /> | File             | Position | Binlog_Do_DB    | Binlog_Ignore_DB |<br /> +------------------+----------+-----------------+------------------+<br /> | mysql-bin.000002 |     1337 | exampledb       |                  |<br /> +------------------+----------+-----------------+------------------+<br /> 1 row in set (0.00 sec)</code></pre></div>    </div>  <div class="paragraph" style="text-align:left;">Write down the output. Bring the MySQL console to the background (press CTRL+Z) and make the database dump:<br /></div>  <div><div id="260144281943121722" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre>$ mysqldump -h localhost -u root -p --opt exampledb &gt; ~/mysql-tutorial/exampledb.sql<br /> $ fg</pre></div>    </div>  <div class="paragraph" style="text-align:left;">`fg` returns you to the MySQL shell, execute the following SQL to finish up:<br /></div>  <div><div id="571173363124408460" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>UNLOCK TABLES;<br /> quit;</code></pre></div>    </div>  <div class="paragraph" style="text-align:left;">Copy exampledb.sql, ca-cert.pem, client-cert.pem and client-key.pem from the ~/mysql-tutorial/ directory to the slave server located at slave.webdevelopersdiary.com. Convince yourself there is no firewall running that is blocking SQL connections from the outside. That's it for the master server.<br /></div>  <h2 style="text-align:left;">Set up the slave server</h2>  <div class="paragraph" style="text-align:left;">The steps for the slave server are:<br /><ol><li>Setup the SSL certificates.</li><li>Edit MySQL server configuration my.cnf.</li><li>Restart MySQL server.</li><li>Configure and start slave server.</li><li>Finish up.</li></ol><br />Place all the .pem files you copied from master server into the directory /etc/mysql/ on the slave server. Start of your favourite editor and edit /etc/mysql/my.cnf and add the following lines:</div>  <div><div id="754805777779792120" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>server-id=2<br /> replicate-do-db=exampledb</code></pre></div>    </div>  <div class="paragraph" style="text-align:left;">Save and close /etc/mysql/my.cnf and restart the MySQL server. Then start up the MySQL console:</div>  <div><div id="281325570488657005" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre>$ sudo service mysql restart<br /> $ mysql -h localhost -u root -p</pre></div>    </div>  <div class="paragraph" style="text-align:left;">Execute following SQL to import database and start slave. Make sure you replace the correct values at MASTER_HOST, MASTER_USER, MASTER_PASSWORD, MASTER_LOG_FILE (from earlier written down value) and MASTER_LOG_POS (also from earlier written down value).<br /></div>  <div><div id="540278042931178819" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>STOP SLAVE;<br /> CREATE DATABASE IF NOT EXISTS exampledb;<br /> USE exampledb;<br /> SOURCE /path/to/exampledb.sql;<br /> <br /> CHANGE MASTER TO<br /> MASTER_HOST='master.webdevelopersdiary.com',<br /> MASTER_USER='slave_user',<br /> MASTER_PASSWORD='slave_password',<br /> MASTER_CONNECT_RETRY=60,<br /> MASTER_LOG_FILE='mysql-bin.000002',<br /> MASTER_LOG_POS=1337,<br /> MASTER_SSL=1,<br /> MASTER_SSL_CA='/etc/mysql/ca-cert.pem',<br /> MASTER_SSL_CERT='/etc/mysql/client-cert.pem',<br /> MASTER_SSL_KEY='/etc/mysql/client-key.pem';<br /> START SLAVE;<br /> SHOW SLAVE STATUS \G;</code></pre></div>    </div>  <div class="paragraph" style="text-align:left;">That's it, database replication should be up and running!</div>]]></content:encoded></item><item><title><![CDATA[How to build the Quercus WAR file yourself and install it in your local Maven repository]]></title><link><![CDATA[http://www.webdevelopersdiary.com/blog/how-to-build-the-quercus-war-file-yourself-and-install-it-in-your-local-maven-repository]]></link><comments><![CDATA[http://www.webdevelopersdiary.com/blog/how-to-build-the-quercus-war-file-yourself-and-install-it-in-your-local-maven-repository#comments]]></comments><pubDate>Sun, 08 Jul 2012 20:38:59 GMT</pubDate><category><![CDATA[aws]]></category><category><![CDATA[maven]]></category><category><![CDATA[quercus]]></category><guid isPermaLink="false">http://www.webdevelopersdiary.com/blog/how-to-build-the-quercus-war-file-yourself-and-install-it-in-your-local-maven-repository</guid><description><![CDATA[If you ever need to compile Quercus from source and install it into your local Maven repository, this post is just for you. You need to have Subversion (or Git), Maven and Ant installed. Here are the steps:   svn co svn://svn.caucho.com/home/svn/svnroot/resin/trunk resin cd resin ant quercus.war mvn install:install-file -Dfile=dist/quercus-4_0-snap.war -DgroupId=com.caucho -DartifactId=quercus -Dversion=4.0-SNAPSHOT -Dpackaging=war       Some time ago I had to download the Resin/Quercus source f [...] ]]></description><content:encoded><![CDATA[<div class="paragraph" style="text-align:left;">If you ever need to compile Quercus from source and install it into your local Maven repository, this post is just for you. You need to have Subversion (or Git), Maven and Ant installed. Here are the steps:</div>  <div><div id="633044179492697709" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre> svn co svn://svn.caucho.com/home/svn/svnroot/resin/trunk resin<br /> cd resin<br /> ant quercus.war<br /> mvn install:install-file -Dfile=dist/quercus-4_0-snap.war -DgroupId=com.caucho -DartifactId=quercus -Dversion=4.0-SNAPSHOT -Dpackaging=war </pre></div>    </div>  <div class="paragraph" style="text-align:left;">Some time ago I had to download the Resin/Quercus source from an unofficial&nbsp;<a href="https://github.com/mdaniel/svn-caucho-com-resin" target="_blank" title="" style="">mirror repository on github</a>, because the official Caucho SVN repository located at svn.caucho.com was down. This mirror repository is still available, but at the time of writing it seems a little outdated. If you are interested, perform these steps instead:<br /></div>  <div><div id="554649029781386951" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre> git clone https://github.com/mdaniel/svn-caucho-com-resin.git<br /> cd svn-caucho-com-resin<br /> ant quercus.war<br /> mvn install:install-file -Dfile=dist/quercus-4_0-snap.war -DgroupId=com.caucho -DartifactId=quercus -Dversion=4.0-SNAPSHOT -Dpackaging=war </pre></div>    </div>  <div class="paragraph" style="text-align:left;">The Quercus webapp is now installed in your local Maven repository as version 4.0-SNAPSHOT. Include in your project's pom.xml as follows:</div>  <div><div id="581781003740805460" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>&lt;project&gt;<br /> &#09;...<br /> &#09;&lt;dependencies&gt;<br /> &#09;&#09;...<br /> &#09;&#09;&lt;dependency&gt;<br /> &#09;&#09;&#09;&lt;groupId&gt;com.caucho&lt;/groupId&gt;<br /> &#09;&#09;&#09;&lt;artifactId&gt;quercus&lt;/artifactId&gt;<br /> &#09;&#09;&#09;&lt;version&gt;4.0-SNAPSHOT&lt;/version&gt;<br /> &#09;&#09;&#09;&lt;type&gt;war&lt;/type&gt;<br /> &#09;&#09;&lt;/dependency&gt;<br /> &#09;&#09;...<br /> &#09;&lt;/dependencies&gt;<br /> &#09;...<br /> &lt;/project&gt;</code></pre></div>    </div>  <div class="paragraph" style="text-align:left;">If you would like to see the other build targets for Resin/Quercus, execute the following command:</div>  <div><div id="772730528205406868" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>ant -p</code></pre></div>    </div>  <div class="paragraph" style="text-align:left;"><strong>Bonus tip</strong>: you can always find a recent version of the Quercus WAR file at http://www.caucho.com/download/quercus-4.0.x.war, replacing 'x' with the latest version, e.g.&nbsp;<a href="http://www.caucho.com/download/quercus-4.0.28.war">http://www.caucho.com/download/quercus-4.0.28.war</a>. You can find the latest version number by looking at Caucho Resin's version number on the <a href="http://www.caucho.com/download/" target="_blank">website's download page</a>. I figured this out once by guessing URLs to download the latest version. The Quercus website is not updated frequently enough to keep up with newer releases, but the files are actually there.</div>]]></content:encoded></item><item><title><![CDATA[How to build H2 database engine yourself and install it in your local Maven repository]]></title><link><![CDATA[http://www.webdevelopersdiary.com/blog/how-to-build-h2-database-engine-yourself-and-install-it-in-your-local-maven-repository]]></link><comments><![CDATA[http://www.webdevelopersdiary.com/blog/how-to-build-h2-database-engine-yourself-and-install-it-in-your-local-maven-repository#comments]]></comments><pubDate>Sun, 08 Jul 2012 19:25:00 GMT</pubDate><category><![CDATA[h2database]]></category><category><![CDATA[maven]]></category><guid isPermaLink="false">http://www.webdevelopersdiary.com/blog/how-to-build-h2-database-engine-yourself-and-install-it-in-your-local-maven-repository</guid><description><![CDATA[If you ever find the need to compile H2 database yourself to install it in your local Maven repository, here are the steps:   svn checkout http://h2database.googlecode.com/svn/trunk/ h2database-read-only cd h2database-read-only/h2 ./build.sh mavenInstallLocal       Your custom H2 build is now installed in your local Maven repository as version 1.0-SNAPSHOT. Include it as a dependency in your project by adding the appropriate XML to your pom.xml:  &lt;project&gt; &#09;... &#09;&lt;dependencies&gt [...] ]]></description><content:encoded><![CDATA[<div class="paragraph" style="text-align:left;">If you ever find the need to compile H2 database yourself to install it in your local Maven repository, here are the steps:</div>  <div><div id="856782530190030112" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre> svn checkout http://h2database.googlecode.com/svn/trunk/ h2database-read-only<br /> cd h2database-read-only/h2<br /> ./build.sh mavenInstallLocal<br /> </pre></div>    </div>  <div class="paragraph" style="text-align:left;">Your custom H2 build is now installed in your local Maven repository as version 1.0-SNAPSHOT. Include it as a dependency in your project by adding the appropriate XML to your pom.xml:</div>  <div><div id="662015503548919271" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>&lt;project&gt;<br /> &#09;...<br /> &#09;&lt;dependencies&gt;<br /> &#09;&#09;...<br /> &#09;&#09;&lt;dependency&gt;<br /> &#09;&#09;&#09;&lt;groupId&gt;com.h2database&lt;/groupId&gt;<br /> &#09;&#09;&#09;&lt;artifactId&gt;h2&lt;/artifactId&gt;<br /> &#09;&#09;&#09;&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;<br /> &#09;&#09;&lt;/dependency&gt;<br /> &#09;&#09;...<br /> &#09;&lt;/dependencies&gt;<br /> &#09;...<br /> &lt;/project&gt;</code></pre></div>    </div>  <div class="paragraph" style="text-align:left;">I had to do these steps in order to test a fix I am preparing for H2 to solve the "SET NAMES" syntax error in MySQL compatibility mode when connecting to H2 using the Quercus PHP MySQL driver.</div>]]></content:encoded></item><item><title><![CDATA[Lightweight self-contained database: SQLite vs. H2 database vs. MySQL embedded]]></title><link><![CDATA[http://www.webdevelopersdiary.com/blog/lightweight-self-contained-database-sqlite-vs-h2-database-vs-mysql-embedded]]></link><comments><![CDATA[http://www.webdevelopersdiary.com/blog/lightweight-self-contained-database-sqlite-vs-h2-database-vs-mysql-embedded#comments]]></comments><pubDate>Sun, 08 Jul 2012 13:16:36 GMT</pubDate><category><![CDATA[h2database]]></category><category><![CDATA[mysql]]></category><category><![CDATA[php]]></category><guid isPermaLink="false">http://www.webdevelopersdiary.com/blog/lightweight-self-contained-database-sqlite-vs-h2-database-vs-mysql-embedded</guid><description><![CDATA[SQLite is a popular choice for PHP programmers to&nbsp;kick-start&nbsp;their development. It's easy, self-contained, requires no configuration, no server and all the data is kept neatly in just one file. Then later on, the application is commonly migrated to a production DBMS (e.g. MySQL). There have not been any alternatives for SQLite when developing in PHP with MySQL as a target platform. But then again, why would you want an alternative for SQLite? Well, to name a few reasons: MySQL compatib [...] ]]></description><content:encoded><![CDATA[<div class="paragraph" style='text-align:left;'>SQLite is a popular choice for PHP programmers to&nbsp;kick-start&nbsp;their development. It's easy, self-contained, requires no configuration, no server and all the data is kept neatly in just one file. Then later on, the application is commonly migrated to a production DBMS (e.g. MySQL). There have not been any alternatives for SQLite when developing in PHP with MySQL as a target platform. But then again, why would you want an alternative for SQLite? Well, to name a few reasons: MySQL compatibility and support for hash indices. And that's not all! Keep reading to see why.<br /><br />When looking for alternatives to SQLite, I came across two suitable candidates: H2 database engine and a MySQL embedded version. My criteria were that the alternative should be as easy to use as SQLite and compatible with MySQL, based on two reasons. First, I wanted it to be compatible with MySQL so that I can keep using that easy self-contained database for development even after the application has gone into production. Second, an easy to setup database is great for bringing new developers up to speed with development as fast as possible.<br /><br />Let's start with the good stuff.&nbsp;Below is a comparison of SQLite, H2 database engine and MySQL embedded version.</div>  <div><div id="318984094507847856" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><table> 	<caption align="bottom">Comparison of self-contained PHP development databases.</caption> 	<col style="text-align: left;" /> 	<col style="text-align: center;" /> 	<col style="text-align: center;" /> 	<thead> 		<tr style="text-align: left;"> 			<th>Compare</th> 			<th>SQLite</th> 			<th>H2 database engine</th> 			<th>MySQL Embedded</th> 		</tr> 	</thead> 	<tbody> 		<tr> 			<td>Footprint</td> 			<td><a href="http://www.sqlite.org/features.html" target="_blank" rel="nofollow">350KiB</a></td> 			<td><a href="http://www.h2database.com/html/features.html#feature_list" target="_blank" rel="nofollow">~1MB</a></td> 			<td><a href="http://www.mysql.com/why-mysql/topreasons_pm.html" target="_blank" rel="nofollow">&lt;2MB</a></td> 		</tr> 		<tr> 			<td>License</td> 			<td><a href="http://www.sqlite.org/copyright.html" target="_blank" rel="nofollow">Public domain</a></td> 			<td><a href="http://www.h2database.com/html/license.html" target="_blank" rel="nofollow">Dual: Modified MPL 1.1 / EPL 1.0</a> (commercial friendly)</td> 			<td><a href="http://www.mysql.com/about/legal/licensing/oem/" target="_blank" rel="nofollow">GPL 2.0</a> (only commercial friendly <a href="http://stackoverflow.com/questions/225987/can-someone-explain-mysqls-license-and-what-it-means-to-closed-source-developme" target="_blank" rel="nofollow">if not redistributed</a>)</td> 		</tr> 		<tr> 			<td>Self-contained</td> 			<td>&#10004;</td> 			<td>&#10004;</td> 			<td>&#10004;</td> 		</tr> 		<tr> 			<td>Single file</td> 			<td>&#10004;</td> 			<td>&#10004;</td> 			<td>&#10006;</td> 		</tr> 		<tr> 			<td>Serverless</td> 			<td>&#10004;</td> 			<td>&#10004;</td> 			<td>&#10006;</td> 		</tr> 		<tr> 			<td>Server-mode</td> 			<td>&#10006;</td> 			<td>&#10004;</td> 			<td>&#10004;</td> 		</tr> 		<tr> 			<td>Zero-configuration</td> 			<td>&#10004;</td> 			<td>&#10004;</td> 			<td>&#10004;</td> 		</tr> 		<tr> 			<td>Transactions</td> 			<td>&#10004;</td> 			<td>&#10004;</td> 			<td>&#10004;</td> 		</tr> 		<tr> 			<td>Indices</td> 			<td>&#10004; (B-tree, R-tree, full-text)</td> 			<td>&#10004; (B-tree, tree, hash, full-text)</td> 			<td>&#10004; (B-tree, R-tree, hash, full-text)</td> 		</tr> 		<tr> 			<td>MySQL compatibility</td> 			<td>&#10006;</td> 			<td>&#10004; (but not 100%)</td> 			<td>&#10004;</td> 		</tr> 		<tr> 			<td>Compatibility with other DBMS</td> 			<td>&#10006;</td> 			<td>&#10004; MySQL, PostgreSQL, Oracle, MSSQL, DB2, HSQLDB and Derby</td> 			<td>&#10006;</td> 		</tr> 		<tr> 			<td>Encryption</td> 			<td>&#10006;</td> 			<td>&#10004;</td> 			<td>&#10006;</td> 		</tr> 		<tr> 			<td>In-memory databases</td> 			<td>&#10004;</td> 			<td>&#10004;</td> 			<td>&#10004; (MEMORY storage engine)</td> 		</tr> 	</tbody> </table> </div>    </div>  <div class="paragraph" style='text-align:left;'>To me it looks like H2 database is the easiest to manage. It requires no server and everything is put neatly in one file, which makes&nbsp;back-upping&nbsp;and sharing databases among developers easier. It even has a nice extra feature over MySQL: encryption of data files (though I don't see a direct need for that during development for me personally).<br /><br />I have <a href="http://www.webdevelopersdiary.com/1/post/2012/07/run-code-igniter-on-h2-database-engine-using-pdo-and-quercus.html" title="">experimented with PHP and H2 before</a> and ran into some limitations (Quercus' MySQL driver doesn't play well yet with H2's MySQL compatibility mode so I have to use the Quercus' PDO driver instead). I haven't tried embedding MySQL yet, which is what I am going to try next.<br /><br />The embedded version of MySQL has the big benefit of being 100% MySQL compatible, but it also has an uncertainty. I am not sure if it's okay to redistribute my application (containing embedded MySQL)&nbsp;among&nbsp;developers. Another limitation could be that I would have to buy a commercial license for MySQL should I choose to distribute embedded MySQL to customers (vs. customers setting up their own MySQL server). Though, it is okay to use MySQL embedded version for a website hosted by myself (it's not technically distributed in that case).<br /><br />For now, I have not decided yet whether I will stick with H2 or switch to the embedded version of MySQL. I am going to give both a try and I will keep you updated via this blog!</div>]]></content:encoded></item><item><title><![CDATA[Run Code Igniter on H2 database engine using PDO and Quercus]]></title><link><![CDATA[http://www.webdevelopersdiary.com/blog/run-code-igniter-on-h2-database-engine-using-pdo-and-quercus]]></link><comments><![CDATA[http://www.webdevelopersdiary.com/blog/run-code-igniter-on-h2-database-engine-using-pdo-and-quercus#comments]]></comments><pubDate>Fri, 06 Jul 2012 10:26:40 GMT</pubDate><category><![CDATA[code igniter]]></category><category><![CDATA[h2database]]></category><category><![CDATA[jamp]]></category><category><![CDATA[php]]></category><category><![CDATA[quercus]]></category><guid isPermaLink="false">http://www.webdevelopersdiary.com/blog/run-code-igniter-on-h2-database-engine-using-pdo-and-quercus</guid><description><![CDATA[In my previous post I explained how to setup PHP and H2 database engine using a platform-independent Java stack, a.k.a. JAMP. I managed to get the Code Igniter framework running on JAMP, with some tweaking of the Code Igniter PDO driver. Here are the steps to make it work.  If you would like to skip these steps and jump immediately into some coding action, download the demo.(Unzip and go into directory jamp-ci-demo/, then type `mvn jetty:run` from the command line. You need Maven installed to ex [...] ]]></description><content:encoded><![CDATA[<div class="paragraph" style='text-align:left;'>In my previous post I explained <a href="http://www.webdevelopersdiary.com/1/post/2012/07/jamp-an-ultra-portable-php-web-server-and-database-stack-in-java.html" title="">how to setup PHP and H2 database engine</a> using a platform-independent Java stack, a.k.a. JAMP. I managed to get the Code Igniter framework running on JAMP, with some tweaking of the Code Igniter PDO driver. Here are the steps to make it work.</div>  <div class="paragraph" style='text-align:left;'>If you would like to skip these steps and jump immediately into some coding action, download the demo.<br />(Unzip and go into directory jamp-ci-demo/, then type `mvn jetty:run` from the command line. You need Maven installed to execute this command.)</div>  <div><div style="margin: 10px 0 0 -10px"> <a href="http://www.webdevelopersdiary.com/uploads/1/2/7/7/12771307/jamp-ci-demo.zip"><img src="http://www.weebly.com/weebly/images/file_icons/gz.png" width="36" height="36" style="float: left; position: relative; left: 0px; top: 0px; margin: 0 15px 15px 0; border: 0;" /></a><div style="float: left; text-align: left; position: relative;"><table style="font-size: 12px; font-family: tahoma; line-height: .9;"><tr><td colspan="2"><b> jamp-ci-demo.zip</b></td></tr><tr style="display: none;"><td>File Size:  </td><td>2341 kb</td></tr><tr style="display: none;"><td>File Type:  </td><td> zip</td></tr></table><a href="http://www.webdevelopersdiary.com/uploads/1/2/7/7/12771307/jamp-ci-demo.zip" style="font-weight: bold;">Download File</a></div> </div>  <hr style="clear: both; width: 100%; visibility: hidden"></hr></div>  <div class="paragraph" style='text-align:left;'>First, checkout a copy of JAMP at <a href="https://github.com/webdevelopersdiary/jamp" target="_blank">github</a> (git clone&nbsp;https://github.com/webdevelopersdiary/jamp.git).<br /><br />Then download Code Igniter and put in the web root (src/main/webapp/).<br /><br />Because Code Igniter likes pretty urls (ie. /controller_name/method_name) I added support for mod_rewrite to JAMP. It can parse mod_rewrite rules which are located in .htaccess directly in the web root (src/main/webapp/). Add src/main/webbapp/.htaccess with mod_rewrite rules:<br /></div>  <div><div id="372713179426368764" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>&lt;IfModule mod_rewrite.c&gt;<br /> &#09;RewriteEngine On<br /> <br /> &#09;RewriteCond %{REQUEST_URI} ^(system|application).*<br /> &#09;RewriteRule ^(.*)$ /index.php?/$1 [L]<br /> <br /> &#09;RewriteCond %{REQUEST_FILENAME} !-f<br /> &#09;RewriteCond %{REQUEST_FILENAME} !-d<br /> &#09;RewriteRule ^(.*)$ index.php?/$1 [L]<br /> &lt;/IfModule&gt;<br /> <br /> &lt;IfModule !mod_rewrite.c&gt;<br /> &#09;ErrorDocument 404 /index.php<br /> &lt;/IfModule&gt;</code></pre></div>    </div>  <div class="paragraph" style='text-align:left;'>Start up the JAMP web server (make sure .htaccess exists before starting the web server or it will have trouble discovering it, then execute mvn jetty:run). Verify Code Igniter is running correctly by going to&nbsp;http://localhost:8080/welcome&nbsp;(the default controller).<br /><br />If you see a pesky error '$assign_to_config is an undefined variable', go to src/main/webapp/index.php and add<br /></div>  <div><div id="307271728508698670" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>$assign_to_config = array();</code></pre></div>    </div>  <div class="paragraph" style='text-align:left;'>under the section 'CUSTOM CONFIG VALUES' (line 110 of index.php) and that should get rid of the error.<br /><br />Next we configure Code Igniter to connect to the H2 database using PDO. Edit the configuration located at src/main/webapp/application/config/database.php and set these settings:<br /></div>  <div><div id="745436434152555038" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>$db['default']['hostname'] = 'mysql:';<br /> $db['default']['dbdriver'] = 'pdo';</code></pre></div>    </div>  <div class="paragraph" style='text-align:left;'>Next, we have to make a slight modification to Code Igniter's PDO driver.&nbsp;Change the method num_rows() from src/main/webapp/system/database/drivers/pdo/pdo_result.php to:<br /><br /></div>  <div><div id="669759757409421700" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>	/**<br /> 	 * Number of rows in the result set<br /> 	 *<br /> 	 * @access	public<br /> 	 * @return	integer<br /> 	 */<br /> 	function num_rows()<br /> 	{<br /> 		$sql = $this->result_id->queryString;<br /> 		if(NULL === $sql) {<br /> 			$sql = print_r($this->result_id, TRUE);<br /> 			$quercus_preg = '/\Aresource\(PDOStatement\[(.*)\]\)\z/is';<br /> 			$php_preg = '/\APDOStatement Object\s*\(.*=>\s*(.*)\)\s*\z/is';<br /> 			if(preg_match($quercus_preg, $sql)) {<br /> 				// We're running in Quercus<br /> 				$sql = preg_replace($quercus_preg, '\1', $sql);<br /> 			} else if(preg_match($php_preg, $sql)) {<br /> 				// We're running in PHP<br /> 				$sql = trim(preg_replace($php_preg, '\1', $sql));<br /> 			} else {<br /> 				// No clue about what is running this script!<br /> 				$sql = NULL;<br /> 			}<br /> 		}<br /> 		if (is_numeric(stripos($sql, 'SELECT')))<br /> 		{<br /> 			$count = count($this->result_id->fetchAll());<br /> 			$this->result_id->execute();<br /> 			return $count;<br /> 		}<br /> 		else<br /> 		{<br /> 			return $this->result_id->rowCount();<br /> 		}<br /> 	}</code></pre></div>    </div>  <div class="paragraph" style='text-align:left;'>Everything should now be in place to connect to the H2 database using Code Igniter's ActiveRecord library. Let's test it! Create a test controller application/controller/test.php:<br /></div>  <div><div id="127868531972379300" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>&lt;?php<br /> <br /> class Test extends CI_Controller {<br /> &#09;public function index() {<br /> &#09;&#09;$this-&gt;load-&gt;database();<br /> &#09;&#09;$sql = $this-&gt;db-&gt;query('CREATE TABLE IF NOT EXISTS test (value DATETIME)');<br /> &#09;&#09;$sql = $this-&gt;db-&gt;set('value', 'NOW()', FALSE)-&gt;insert('test');<br /> &#09;&#09;var_dump($this-&gt;db-&gt;get('test')-&gt;result());<br /> &#09;}<br /> }</code></pre></div>    </div>  <div class="paragraph" style='text-align:left;'>Voila! Go to http://localhost:8080/test and enjoy a working Code Igniter on H2 database using ActiveRecord :-)<br /><br />Note: I used Code Igniter version 2.1.2, Quercus version 4.0.28 and H2 database version 1.3.167.<br /></div>]]></content:encoded></item><item><title><![CDATA[JAMP: an ultra portable PHP, web server and database stack in Java]]></title><link><![CDATA[http://www.webdevelopersdiary.com/blog/jamp-an-ultra-portable-php-web-server-and-database-stack-in-java]]></link><comments><![CDATA[http://www.webdevelopersdiary.com/blog/jamp-an-ultra-portable-php-web-server-and-database-stack-in-java#comments]]></comments><pubDate>Thu, 05 Jul 2012 18:20:55 GMT</pubDate><category><![CDATA[jamp]]></category><category><![CDATA[java]]></category><category><![CDATA[maven]]></category><category><![CDATA[php]]></category><guid isPermaLink="false">http://www.webdevelopersdiary.com/blog/jamp-an-ultra-portable-php-web-server-and-database-stack-in-java</guid><description><![CDATA[Recently I was experimenting with running PHP and the H2 database engine in a Java web server using Caucho Quercus (or just Quercus for short). In theory this would yield an ultra portable platform-independent PHP, web server and database stack. I eventually got it working, but with some limitations. Before I get into details, here are some quick notes: Quercus is a 100% Java implementation of PHP5, though it doesn't support 100% of PHP5's functionality, unfortunately. Also, Quercus' implementat [...] ]]></description><content:encoded><![CDATA[<div class="paragraph" style='text-align:left;'>Recently I was experimenting with running PHP and the H2 database engine in a Java web server using Caucho Quercus (or just Quercus for short). In theory this would yield an ultra portable platform-independent PHP, web server and database stack. I eventually got it working, but with some limitations. Before I get into details, here are some quick notes: Quercus is a 100% Java implementation of PHP5, though it doesn't support 100% of PHP5's functionality, unfortunately. Also, Quercus' implementation seems to differ from PHP5's implementation sometimes. That said, I thought I would give it a shot anyway, because some popular CMS systems have had success running with Quercus, such as Drupal and Wordpress. I used Jetty as my Java web server (a.k.a. Java web container) and Maven for dependency management. The source code of my experiment is available at <a href="https://github.com/webdevelopersdiary/jamp" target="_blank" title="">github</a>.<br /><br />This tutorial describes how to set up the PHP and database stack in the Jetty web server. First we set up a web application (webapp for short) in Java which can interpret .php files using Quercus. Then we setup the H2 database engine. Last, we setup the part where PHP can connect to H2 while actually thinking it is MySQL that it's connecting to (using H2's MySQL compatibility mode, because PHP does not have support for H2). Here we go!<br /><br /><strong><font size="5">Ultra short walk-through</font></strong><br /><ol><li>Make sure you have Maven installed.</li><li><a href="http://quercus.caucho.com/" target="_blank" title="">Download latest version of Quercus</a> (scroll down, get the WAR file).</li><li>Go to the directory where you downloaded Quercus and install it in your local maven repository by running the Maven command (e.g. for Quercus version 4.0.25):<br />$ mvn install:install-file -Dfile=quercus-4.0.25.war -DgroupId=com.caucho -DartifactId=quercus -Dversion=4.0.25 -Dpackaging=war<br /></li><li>Checkout git repository:<br />$ git clone&nbsp;https://github.com/webdevelopersdiary/jamp.git<br />$ cd jamp</li><li>Run web server:<br />$ mvn jetty:run</li><li>Wait until you see "[INFO] Started Jetty Server" in the console output and point your browser to http://localhost:8080/</li><li>The document root is located in src/main/webapp/</li><li>Connect to the database in your code using "$pdo = new PDO('mysql:');", mysql_connect() doesn't work due to a driver incompatibility, for more information on this keep on reading.</li></ol><br /><strong><font size="5">The long version<br /></font></strong>Setting up Quercus is quite straight-forward. Download the WAR file of the latest version of Quercus (in my case 4.0.25) and install it in your local Maven repository by running this command from the same location as where you downloaded quercus-4.0.25.war:<br /></div>  <div><div id="854088644157526430" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre> mvn install:install-file -Dfile=quercus-4.0.25.war -DgroupId=com.caucho -DartifactId=quercus -Dversion=4.0.25 -Dpackaging=war </pre></div>    </div>  <div class="paragraph" style='text-align:left;'>Then include the following dependency to your Maven project's pom.xml and Quercus should be integrated in your webapp:</div>  <div><div id="137827088939550985" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>&lt;project&gt;<br /> &#09;...<br /> &#09;&lt;dependencies&gt;<br /> &#09;&#09;...<br /> &#09;&#09;&lt;dependency&gt;<br /> &#09;&#09;&#09;&lt;groupId&gt;com.caucho&lt;/groupId&gt;<br /> &#09;&#09;&#09;&lt;artifactId&gt;quercus&lt;/artifactId&gt;<br /> &#09;&#09;&#09;&lt;version&gt;4.0.25&lt;/version&gt;<br /> &#09;&#09;&#09;&lt;type&gt;war&lt;/type&gt;<br /> &#09;&#09;&lt;/dependency&gt;<br /> &#09;&#09;...<br /> &#09;&lt;/dependencies&gt;<br /> &#09;...<br /> &lt;/project&gt;</code></pre></div>    </div>  <div class="paragraph" style='text-align:left;'>The next step is to be able to run the webapp locally, so that's what we use Jetty for. Add this to your pom.xml:</div>  <div><div id="848449862933348235" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>&lt;project&gt;<br /> &#09;...<br /> &#09;&lt;build&gt;<br /> &#09;&#09;&lt;plugins&gt;<br /> &#09;&#09;&#09;&lt;plugin&gt;<br /> &#09;&#09;&#09;&#09;&lt;groupId&gt;org.mortbay.jetty&lt;/groupId&gt;<br /> &#09;&#09;&#09;&#09;&lt;artifactId&gt;jetty-maven-plugin&lt;/artifactId&gt;<br /> &#09;&#09;&#09;&lt;/plugin&gt;<br /> &#09;&#09;&lt;/plugins&gt;<br /> &#09;&#09;&lt;pluginManagement&gt;<br /> &#09;&#09;&#09;&lt;plugins&gt;<br /> &#09;&#09;&#09;&#09;&lt;plugin&gt;<br /> &#09;&#09;&#09;&#09;&#09;&lt;groupId&gt;org.mortbay.jetty&lt;/groupId&gt;<br /> &#09;&#09;&#09;&#09;&#09;&lt;artifactId&gt;jetty-maven-plugin&lt;/artifactId&gt;<br /> &#09;&#09;&#09;&#09;&#09;&lt;version&gt;8.1.4.v20120524&lt;/version&gt;<br /> &#09;&#09;&#09;&#09;&#09;&lt;configuration&gt;<br /> &#09;&#09;&#09;&#09;&#09;&#09;&lt;scanIntervalSeconds&gt;1&lt;/scanIntervalSeconds&gt;<br /> &#09;&#09;&#09;&#09;&#09;&#09;&lt;stopKey&gt;foo&lt;/stopKey&gt;<br /> &#09;&#09;&#09;&#09;&#09;&#09;&lt;stopPort&gt;9999&lt;/stopPort&gt;<br /> &#09;&#09;&#09;&#09;&#09;&lt;/configuration&gt;<br /> &#09;&#09;&#09;&#09;&lt;/plugin&gt;<br /> &#09;&#09;&#09;&lt;/plugins&gt;<br /> &#09;&#09;&lt;/pluginManagement&gt;<br /> &#09;&lt;/build&gt;<br /> &#09;...<br /> &lt;/project&gt;</code></pre></div>    </div>  <div class="paragraph" style='text-align:left;'>Now Jetty and Quercus should be okay to go. You can verify this by putting a phpinfo.php file with the famous code snippet "&lt;?php phpinfo(); ?&gt;" at src/main/webapp/phpinfo.php (relative to pom.xml's location, create directories if they don't exist) and start up the Jetty webserver:</div>  <div><div id="493852029184701513" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre> echo '&lt;?php phpinfo();' &gt; src/main/webapp/phpinfo.php<br /> mvn jetty:run<br /> </pre></div>    </div>  <div class="paragraph" style='text-align:left;'>Wait for Jetty to be completely started (console output should say something like "[INFO] Started Jetty Server") and point your browser to http://localhost:8080/phpinfo.php and verify that Jetty is running and Quercus is installed. (You can stop Jetty by pressing CTRL-C in the console.)<br /><br />Congratulations! If you made it this far, you have successfully setup a Java web server that can interpret PHP files! But, we're not there yet, we still have to add database support.</div>  <div class="paragraph" style='text-align:left;'>We will run the H2 database engine in embedded mode. To do this, we need to include the H2 dependency in the pom.xml. For the integration of H2 and PHP another dependency is required as well: database connection pooling (DBCP), so add both to your pom.xml:</div>  <div><div id="975104053397500990" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>&lt;project&gt;<br /> &#09;...<br /> &#09;&lt;dependencies&gt;<br /> &#09;&#09;...<br /> &#09;&#09;&lt;dependency&gt;<br /> &#09;&#09;&#09;&lt;groupId&gt;com.h2database&lt;/groupId&gt;<br /> &#09;&#09;&#09;&lt;artifactId&gt;h2&lt;/artifactId&gt;<br /> &#09;&#09;&#09;&lt;version&gt;1.3.167&lt;/version&gt;<br /> &#09;&#09;&lt;/dependency&gt;<br /> &#09;&#09;&lt;dependency&gt;<br /> &#09;&#09;&#09;&lt;groupId&gt;commons-dbcp&lt;/groupId&gt;<br /> &#09;&#09;&#09;&lt;artifactId&gt;commons-dbcp&lt;/artifactId&gt;<br /> &#09;&#09;&#09;&lt;version&gt;1.4&lt;/version&gt;<br /> &#09;&#09;&lt;/dependency&gt;<br /> &#09;&#09;...<br /> &#09;&lt;/dependencies&gt;<br /> &#09;...<br /> &lt;/project&gt;</code></pre></div>    </div>  <div class="paragraph" style='text-align:left;'>To actually integrate H2 and PHP we need to tell Jetty some additional configuration. Get these two files (<a href="https://raw.github.com/webdevelopersdiary/jamp/master/src/main/webapp/WEB-INF/web.xml" target="_blank">web.xml</a> and <a href="https://raw.github.com/webdevelopersdiary/jamp/master/src/main/webapp/WEB-INF/jetty-env.xml" target="_blank">jetty-env.xml</a>) and drop them in your src/main/webapp/WEB-INF/ directory (create this directory if it doesn't exist).</div>  <div class="paragraph" style='text-align:left;'>At this point, your PHP webapp is entirely configured to connect an in-memory embedded H2 database. <strong>But, there is a catch.</strong> At the point of writing this article, H2's MySQL compatibility mode is not compatible with Quercus' implementation of the PHP MySQL driver (any attempt to connect to H2 using PHP's mysql_connect() will result in an error complaining about a syntax error in query "SET NAMES ..."). To work around this, we connect to H2 using Quercus' implementation of the PHP PDO driver, which does seem to be compatible with H2's MySQL compatibility mode. So, create a file, e.g. test-database.php and put it in src/main/webapp/test-database.php:</div>  <div><div id="153241869284734006" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>&lt;?php<br /> <br /> $pdo = new PDO('mysql:');<br /> $sth = $pdo->prepare('SELECT NOW()');<br /> if($sth) {<br /> &#09;$sth->execute();<br /> &#09;if($now = $sth->fetchColumn()) {<br /> &#09;&#09;printf('Successfully executed query on H2 at %s.', $now);<br /> &#09;} else {<br /> &#09;&#09;echo 'Could not execute query.';<br /> &#09;}<br /> } else {<br /> &#09;echo 'Could not prepare query.';<br /> }</code></pre></div>    </div>  <div class="paragraph" style='text-align:left;'>Go to http://localhost:8080/test-database.php to see if it's working (which if everything went okay, it should!).<br /><br />So now you should have your ultra-portable platform-independent PHP, web server and database stack up and running. Happy experimenting! :-)</div>  <div class="paragraph" style='text-align:left;'><strong><font size="4">What's next?</font></strong><br />I would like to see a working version of Code Igniter using the ActiveRecord class running on this stack. Also, it would be nice to have H2's MySQL compatibility mode compatible with Quercus' MySQL driver, so we can run applications that use mysql_connect() on the stack (e.g. phpMyAdmin).</div>  <div class="paragraph" style='text-align:left;'><strong style=""><font size="4">Trouble shooting</font></strong><br />If you want to call PDOStatement::fetchObject(), you should call PDOStatement::fetchObject('stdClass') instead. This is a bug in Quercus (remember I said in the beginning that sometimes Quercus' implementation differs from PHP's). Acccording to the PHP Manual, the default first argument for PDOStatement::fetchObject should be 'stdClass', it seems the folks at Caucho forgot to implement this.<br /><br />If you want to use a persistent database file instead of the temporary in-memory database, go to src/main/webapp/WEB-INF/jetty-env.xml and change line</div>  <div><div id="228073031145071985" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>&lt;Set name="url"&gt;jdbc:h2:mem:;MODE=MYSQL&lt;/Set&gt;</code></pre></div>    </div>  <div class="paragraph" style='text-align:left;'>to</div>  <div><div id="473232430320535778" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre><code>&lt;Set name="url"&gt;jdbc:h2:filename;MODE=MYSQL&lt;/Set&gt;</code></pre></div>    </div>  <div class="paragraph" style='text-align:left;'>Where you replace 'filename' with a relative path to a file (relative to pom.xml) or an absolute path to a file. Note that "mem:" has an extra ":" after it and "filename" does not. For more information about the H2 JDBC URL see <a href="http://www.h2database.com/html/features.html#database_url" target="_blank" title="">H2's feature list</a>.</div>]]></content:encoded></item><item><title><![CDATA[Good to know: how to properly store date and time values in MySQL]]></title><link><![CDATA[http://www.webdevelopersdiary.com/blog/good-to-know-how-to-properly-store-date-and-time-values-in-mysql]]></link><comments><![CDATA[http://www.webdevelopersdiary.com/blog/good-to-know-how-to-properly-store-date-and-time-values-in-mysql#comments]]></comments><pubDate>Sat, 30 Jun 2012 19:32:37 GMT</pubDate><category><![CDATA[mysql]]></category><guid isPermaLink="false">http://www.webdevelopersdiary.com/blog/good-to-know-how-to-properly-store-date-and-time-values-in-mysql</guid><description><![CDATA[MySQL has two native field types that can store information about both date and time: timestamp and datetime. The major differences between timestamp and datetime field types are:timestamp can hold values only in the range&nbsp;'1970-01-01 00:00:01'&nbsp;UTC to&nbsp;'2038-01-19 03:14:07'&nbsp;UTC while datetime can hold any value in the range&nbsp;'1000-01-01 00:00:00'&nbsp;to&nbsp;'9999-12-31 23:59:59'.timestamp is the only mysql field type that stores values relative to a timezone (namely UTC) [...] ]]></description><content:encoded><![CDATA[<div class="paragraph" style='text-align:left;'>MySQL has two native field types that can store information about both date and time: <span style="font-weight: bold;">timestamp</span> and <span style="font-weight: bold;">datetime</span>. The major differences between <strong>timestamp</strong> and <strong>datetime</strong> field types are:<br /><ol><li><strong>timestamp</strong> can hold values only in the range&nbsp;'1970-01-01 00:00:01'&nbsp;UTC to&nbsp;'2038-01-19 03:14:07'&nbsp;UTC while <strong>datetime</strong> can hold any value in the range&nbsp;'1000-01-01 00:00:00'&nbsp;to&nbsp;'9999-12-31 23:59:59'.</li><li><strong>timestamp</strong> is the only mysql field type that stores values relative to a timezone (namely UTC), <strong>datetime</strong> does not store according to a time zone!</li></ol><br />This has some serious implications. First, <strong>timestamp</strong> can only be used where the supported value range is sufficient. Second, for scenarios where you would want to log, compare or perform other tasks with date and time,&nbsp;<strong style="font-weight: normal;"><span style="font-weight: bold;">datetime</span></strong>&nbsp;is not reliable if you adhere to your server time zone settings because <strong>datetime</strong> does not store the time zone information. An example to illustrate this unreliability:<br /><br />Suppose you are logging transactions of some sort to your MySQL database, neatly adding the date and time at which the transaction took place, relying on the server time zone settings. Another process is monitoring the transactions (e.g. is client X not exceeding Y transactions per hour, whether transactions result in insufficient account balance, etc.). Now suppose the time zone of the server changes, this could be because of any reason:<br /><ol><li>Some countries have daylight savings time, if your server is configured to that country's particular timezone, your server automatically changes time zone upon entering and leaving daylight savings time.<br /></li><li>The server administrator manually changes the time zone, e.g. because the server moved physically, perhaps the change is by accident or he/she decided it was better to change to a time zone that doesn't have daylight savings time ;-)</li><li>A bug in the system software changes the time zone by accident.</li></ol><br />To stick with the daylight savings example, let's say the clock gets rewound one hour. Transactions are still happily being inserted into the database using <strong>datetime</strong>. Now, according to the transaction log, customers are making twice the amount of transactions during this hour. But wait?! Weren't we keeping an eye on the maximum amount of transactions per hour of clients? It looks like some clients that are operating at only half of their capacity suddenly exceed the maximum transaction limit during that hour, uh oh!<br /><br />How will MySQL know whether it was 2:30 AM before or after daylight saving time when you use <strong>timestamp</strong>? The answer is: <span style="text-decoration: underline;">MySQL doesn't know</span>! E.g. 28 October 2012 2:30 AM in either CEST (UTC+2:00) or CET (UTC+1:00) are both stored as unix timestamp value 1351387800 (= "2012-10-28 02:30 CET" = "2012-10-28 01:30 UTC"). As we will see soon, this is <a title="" target="_blank" href="http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp">documented behaviour</a>. It's not possible to insert unix timestamps directly into a <span style="font-weight: bold;">timestamp</span> field, so you are required to use a text representation of the date and time you are trying to store (e.g. "2012-10-28 2:30", NOW(), CURRENT_TIMESTAMP). MySQL takes this string and converts it into an unix timestamp using UNIX_TIMESTAMP(), this is where the point where 28 October 2012 2:30 AM CEST and CET both get mapped to 1351387800. One <a title="" target="_blank" href="https://web.ivy.net/%7Ecarton/rant/MySQL-timezones.txt">suggested work-around</a> I came accross doesn't work in my boundary case, confirming what the manual says:<br /><span></span><br /></div>  <div><div id="515867752973002090" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre> mysql&gt; SELECT FROM_UNIXTIME(@ts := 1351387800) = FROM_UNIXTIME(@ts - 3600);<br /> +--------------------------------------------------------------+<br /> | FROM_UNIXTIME(@ts := 1351387800) = FROM_UNIXTIME(@ts - 3600) |<br /> +--------------------------------------------------------------+<br /> |                                                            1 |<br /> +--------------------------------------------------------------+<br /> 1 row in set (0.00 sec) </pre></div>    </div>  <div class="paragraph" style='text-align:left;'><br /><span></span>So how can we store our dates and times without being affected by this? The answer lies in designing your database (and application). <span style="text-decoration: underline;">Trusting your server's time zone settings and MySQL's time zone conversion abilities is a bad idea. Instead, use </span><span style="font-weight: bold; text-decoration: underline;">datetime</span><span style="text-decoration: underline;"> fields and store UTC formatted values only.</span>&nbsp; You can always get the current date and time in UTC via UTC_TIMESTAMP(), e.g.:<br /><span></span><br /></div>  <div><div id="569369564597684110" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><pre> mysql&gt; SELECT UNIX_TIMESTAMP(), UTC_TIMESTAMP(), NOW();<br /> +------------------+---------------------+---------------------+<br /> | UNIX_TIMESTAMP() | UTC_TIMESTAMP()     | NOW()               |<br /> +------------------+---------------------+---------------------+<br /> |       1351384200 | 2012-10-28 00:30:00 | 2012-10-28 02:30:00 |<br /> +------------------+---------------------+---------------------+<br /> 1 row in set (0.00 sec) </pre></div>    </div>  <div class="paragraph" style='text-align:left;'><br /><span></span>Alternatively, you could store unix timestamps in unsigned int columns. But then you can not reliably use all those useful documented MySQL date and time functions, and, you would have to write your own date and time calculations using math in queries which can get messy.<br /><br /><span>A disadvantage of storing all values in a <span style="font-weight: bold;">datetime</span> field is that you cannot make use anymore of the CURRENT_TIMESTAMP default value. But then again, </span>if you need to store values outside the <span style="font-weight: bold;">timestamp</span> range or need 100% reliability, that might be one of your lesser concerns.<br /><br /><span style="font-weight: bold;">Conclusion</span><br /><span>Use UTC formatted date and time values within MySQL and your application for reliability</span>&nbsp;&nbsp;&nbsp; <span>. Store all your date and time values in UTC format in a <span style="font-weight: bold;">datetime</span> column</span>.<br /></div>]]></content:encoded></item></channel></rss>