<?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/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Databases and Life &#187; Broken</title>
	<atom:link href="http://www.databasesandlife.com/category/bugs/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.databasesandlife.com</link>
	<description>Adrian Smith's blog</description>
	<lastBuildDate>Mon, 14 Jun 2010 09:41:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Java is lacking a String &#8220;join&#8221; function</title>
		<link>http://www.databasesandlife.com/java-is-lacking-a-string-join-function/</link>
		<comments>http://www.databasesandlife.com/java-is-lacking-a-string-join-function/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 10:49:49 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=465</guid>
		<description><![CDATA[Between Java 1.0 and Java 1.3 (1996-2002 according to Wikipedia) there was no way to split strings into an array or a list.
In Java 1.4 the authors of Java saw it fit to introduce a method to split strings,
String csvData = "field1,field2,field3";
String[] fields = csvData.split(",");

However they did not introduce a method to &#8220;join&#8221; strings! Even [...]]]></description>
			<content:encoded><![CDATA[<p>Between Java 1.0 and Java 1.3 (1996-2002 according to Wikipedia) there was no way to split strings into an array or a list.</p>
<p>In Java 1.4 the authors of Java saw it fit to introduce a method to split strings,</p>
<pre>String csvData = "field1,field2,field3";
String[] fields = csvData.split(",");
</pre>
<p>However they did not introduce a method to &#8220;join&#8221; strings! Even in Java 7 there is no way to do this, e.g. via a static String.join method (2002-now).</p>
<p>OK I realize this is not &#8220;rocket science&#8221;, and I appreciate it exists in various versions in various third-party libraries, but still, it&#8217;s something every program needs to do at some point, it&#8217;s annoying to have to re-define it or think about it for each application.</p>
<p>For example, in one project I was working on in the last 6 months, such a function was created, and then had a bug! (OK but to be fair, that was not the only bug in the application!)</p>
<p>Come on, I mean this is a totally trivial function, totally necessary, available in all scripting lanuags why is it still not in Java?!</p>
<p>(P.S. Want to see an &#8220;enterprise java&#8221; solution to this problem? Check out the <a href="http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/base/Joiner.html#method_summary">number of methods on this class</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/java-is-lacking-a-string-join-function/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java varargs: inconsistent behaviour if you pass an array</title>
		<link>http://www.databasesandlife.com/java-varargs-inconsistent-behaviour-if-you-pass-an-array/</link>
		<comments>http://www.databasesandlife.com/java-varargs-inconsistent-behaviour-if-you-pass-an-array/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 10:39:07 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=461</guid>
		<description><![CDATA[In Java 1.4 there was the function Arrays.asList. You could pass it an array and it would make a list out of it.
String[] myArray = new String[] { "foo", "bar" };
List myList = Arrays.asList(myArray);

In Java 1.5 this was retrofitted for varargs; you could simply pass elements to the function
List&#60;String&#62; myList = Arrays.asList("foo", "bar");

I never really [...]]]></description>
			<content:encoded><![CDATA[<p>In Java 1.4 there was the function Arrays.asList. You could pass it an array and it would make a list out of it.</p>
<pre>String[] myArray = new String[] { "foo", "bar" };
List myList = Arrays.asList(myArray);
</pre>
<p>In Java 1.5 this was retrofitted for varargs; you could simply pass elements to the function</p>
<pre>List&lt;String&gt; myList = Arrays.asList("foo", "bar");
</pre>
<p>I never really understood how that worked in a backwards-compatible way; I mean either the function takes an array of stuff, or it takes individual elements, surely?</p>
<p>It turns out, that with the varargs syntax, the caller is not forced to pass individual elements, the caller can instead pass an array of elements.</p>
<pre>List&lt;String&gt; myList = Arrays.asList("foo", "bar");
List&lt;String&gt; myList = Arrays.asList(new String[] { "foo", "bar" });
</pre>
<p>The above two calls are identical, both return a List&lt;String&gt;.</p>
<p>But surely this is really dangerous? I mean Arrays.asList does not make any assumptions about what types of arguments it accepts; the list can be composed of any object.</p>
<p>How can it be certain that you want to have an List of Strings, and not a List containing a single element which is a String array? (An array is an object.)</p>
<p>To demonstrate this inconsistency:</p>
<pre>String[] arr = new String[] { "foo", "bar" };
Arrays.asList(arr);            // returns List&lt;String&gt;
Arrays.asList(arr, arr);       // returns List&lt;String[]&gt;
Arrays.asList(arr, arr, arr);  // returns List&lt;String[]&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/java-varargs-inconsistent-behaviour-if-you-pass-an-array/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>foreach syntax</title>
		<link>http://www.databasesandlife.com/foreach-syntax/</link>
		<comments>http://www.databasesandlife.com/foreach-syntax/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 13:04:26 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=442</guid>
		<description><![CDATA[Most modern languages use very similar syntax inspired by C; but the features added since C are really non-standard! The &#8220;for-each&#8221; syntax annoys me particularly. I mean none of these is significantly better/worse than the others, but I program in all these languages (apart from C#) on a regular basis and I always have to [...]]]></description>
			<content:encoded><![CDATA[<p>Most modern languages use very similar syntax inspired by C; but the features added since C are really non-standard! The &#8220;for-each&#8221; syntax annoys me particularly. I mean none of these is significantly better/worse than the others, but I program in all these languages (apart from C#) on a regular basis and I always have to think when typing in the line in order not to get the wrong syntax.</p>
<table style="width: 100%;">
<tbody>
<tr>
<td style="width: 100px;"><strong>PHP</strong></td>
<td style="font-family: monospace;">foreach (<em>list </em>as <em>element</em>)</td>
</tr>
<tr>
<td><strong>Perl</strong></td>
<td style="font-family: monospace;">foreach my <em>element </em>(<em>list</em>)</td>
</tr>
<tr>
<td><strong>Java</strong></td>
<td style="font-family: monospace;">for (<em>element </em>: <em>list</em>)</td>
</tr>
<tr>
<td><strong>Javascript</strong></td>
<td style="font-family: monospace;">for (var <em>element </em>in <em>list</em>)</td>
</tr>
<tr>
<td><strong>C#</strong></td>
<td style="font-family: monospace;">foreach (<em>element </em>in <em>list</em>)</td>
</tr>
</tbody>
</table>
<p>For what it&#8217;s worth, I think &#8220;foreach&#8221; is nicer than &#8220;for&#8221; as it reads more like a sentence (the word &#8220;for&#8221; really makes no sense at all in that context); and about &#8220;in&#8221; vs. colon I&#8217;ve got no preference really.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/foreach-syntax/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Upgrade to Lenny, everything down :(</title>
		<link>http://www.databasesandlife.com/upgrade-to-lenny-everything-down/</link>
		<comments>http://www.databasesandlife.com/upgrade-to-lenny-everything-down/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 19:54:52 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=430</guid>
		<description><![CDATA[How annoying, I upgraded from Debian Etch (Apache 2.2.3-4) to Debian Lenny (Apache 2.2.9), and then my Subversion Server (over HTTPS) gave the following error when surfed to from Firefox, which worked fine before:
An error occurred during a connection to svn.example.com.
SSL received a record that exceeded the maximum permissible length.
(Error code: ssl_error_rx_record_too_long)
What does that mean!? [...]]]></description>
			<content:encoded><![CDATA[<p>How annoying, I upgraded from Debian Etch (Apache 2.2.3-4) to Debian Lenny (Apache 2.2.9), and then my Subversion Server (over HTTPS) gave the following error when surfed to from Firefox, which worked fine before:</p>
<blockquote><p>An error occurred during a connection to svn.example.com.<br />
SSL received a record that exceeded the maximum permissible length.<br />
(Error code: ssl_error_rx_record_too_long)</p></blockquote>
<p>What does that mean!? There&#8217;s not a great deal of info on the web.</p>
<p>Fundamentally, in my case, the first thing to work out, is that that error message means (or meant, in my case at least) HTTP was being transmitted over the HTTPS port, i.e. it wasn&#8217;t valid HTTPS at all, thus the protocol error. This could be confirmed by surfing to http://&#8230;:443/ (i.e. not https://) and seeing that the content (the Subversion server in my case) was correct.</p>
<p>The question was why? I had a bunch of sites in the &#8220;sites-enabled&#8221; directory, and another one of them (not my Subversion site!) had a</p>
<pre>&lt;VirtualHost *&gt;
</pre>
<p>whereas it should have been</p>
<pre>&lt;VirtualHost *:80&gt;
</pre>
<p>i.e. the port was missing. I&#8217;m not quite sure why it had that effect, as the request to the Subversion HTTPS URL did deliver the Subversion content, just not over HTTPS any more. But perhaps without the :80, it decided all ports should be subject to NameVirtualHost, and as that&#8217;s not possible with HTTPS, switched HTTPS off for all ports and all sites?</p>
<p>Nightmare &#8230;.</p>
<p>See also: <a href="http://stackoverflow.com/questions/119336/ssl-error-rx-record-too-long-and-apache-ssl">http://stackoverflow.com/questions/119336/ssl-error-rx-record-too-long-and-apache-ssl</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/upgrade-to-lenny-everything-down/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP infinite recursion</title>
		<link>http://www.databasesandlife.com/php-infinite-recursion/</link>
		<comments>http://www.databasesandlife.com/php-infinite-recursion/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 12:50:11 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=388</guid>
		<description><![CDATA[What can I say? How about &#8220;toy language&#8221;?

$ php -r 'function foo() { foo(); } foo();'
Segmentation fault

I&#8217;m not saying that infinite recursion is a good idea, but during development it can happen by accident, and I don&#8217;t expect such a simple error to crash the PHP interpreter! (Also it took me about 20 minutes to [...]]]></description>
			<content:encoded><![CDATA[<p>What can I say? How about &#8220;toy language&#8221;?</p>
<pre>
$ php -r 'function foo() { foo(); } foo();'
Segmentation fault
</pre>
<p>I&#8217;m not saying that infinite recursion is a good idea, but during development it can happen by accident, and I don&#8217;t expect such a simple error to crash the PHP interpreter! (Also it took me about 20 minutes to debug this problem, as I had no idea where it happened, nor indeed what the problem was..)</p>
<p><i>PHP 5.2.6 on Linux 2.6.26 Debian</i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/php-infinite-recursion/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Java: Always explicitly specify which XML parser to use</title>
		<link>http://www.databasesandlife.com/java-always-explicitly-specify-which-xml-parser-to-use/</link>
		<comments>http://www.databasesandlife.com/java-always-explicitly-specify-which-xml-parser-to-use/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 12:42:26 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=351</guid>
		<description><![CDATA[There is the following design error in Java (at least in Servlets):

A server may serve multiple applications; each application may use different libraries or even different versions of the same library, &#8220;side by side&#8221;.
XML parsers, transformers (XSLT), etc., have a standard interface, and there may be different implementations of this interface from different vendors, open-source [...]]]></description>
			<content:encoded><![CDATA[<p>There is the following design error in Java (at least in Servlets):</p>
<ol>
<li>A server may serve multiple applications; each application may use different libraries or even different versions of the same library, &#8220;side by side&#8221;.</li>
<li>XML parsers, transformers (XSLT), etc., have a standard interface, and there may be different implementations of this interface from different vendors, open-source projects, etc.</li>
<li>Which XML parser, transformed etc. is actually used depends on a <strong>global system variable</strong>.</li>
</ol>
<p>And it&#8217;s point 3 that&#8217;s the problem really. Points 1 and 2 are debatable: they certainly bring advantages, but they certainly bring complexity too.</p>
<p>I just had the problem that one of my web applications stopped working, but only intermittently. Restarting the server led to everything being OK, but later things would not be OK. I do hate environments where everything appears to work, yet in fact doesn&#8217;t. I mean how do you know when you&#8217;re &#8220;done&#8221; in such an environment? (Or how do you even know you are in such an environment?)</p>
<p>The bug was caused by:</p>
<ol>
<li>Application one used the default XML parser, and didn&#8217;t have any extra JARs (libraries) for reading XML</li>
<li>Application two required a special XML parser, set the global variable so it would be used, and included the JARs necessary for the special XML parser</li>
</ol>
<p>So when a request came to application 1, <em>after </em>a request had come to application 2, then the system would try to instantiate the special XML parser within application 1 (specified in the global variable set by application 2), but wouldn&#8217;t find it, as it wasn&#8217;t deployed in application 1 (and applications can&#8217;t use one another&#8217;s libraries, due to feature #1).</p>
<p>This seems obvious when one describes it, but looking at the logs, on a live server, with the system down and the clock ticking? &#8211; Far from obvious.</p>
<p>So now, I assert, every time you want to create an XML parser, do the following:</p>
<h3>If you require a special XML library, use:</h3>
<pre>
System.<strong>setProperty</strong>("javax.xml.parsers.DocumentBuilderFactory",
    "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
...
</pre>
<h3>If you require the standard XML library, use:</h3>
<pre>
Properties systemProperties = System.getProperties();
systemProperties.<strong>remove</strong>("javax.xml.parsers.DocumentBuilderFactory");
System.setProperties(systemProperties);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
...
</pre>
<p>There is also the possibility to pass a parameter to DocumentBuilderFactory to specify which XML parser technology to use. That&#8217;s a good option too, as it wouldn&#8217;t &#8220;corrupt&#8221; this global variable (&#8220;system property&#8221;). However I think one should be defensive, and always delete the global variable if one wishes to use the standard XML parser, and therefore it doesn&#8217;t matter if this global variable gets corrupted or not.</p>
<h3>Never do the following:</h3>
<pre>
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
</pre>
<p>This simply relies on whichever XML parser is currently set in the global variable. You have no way to guarantee that some other application running on the same server won&#8217;t set the global variable to use an XML parser you don&#8217;t have installed in your application. Even if you have control of the server and all applications, you don&#8217;t know what software you&#8217;ll be writing in the future. (In this case I installed a new application to a server which&#8217;d been running fine for 1 year, but due to setting the global variable, the old application broke..)</p>
<p>The same applies for all those other &#8220;factory&#8221; situations such as TransformerFactory.newInstance() etc.</p>
<p>This feels all quite inelegant to me, and has just cost me a lot of time, and it&#8217;s not as if I&#8217;m so new to programming Java. I am wondering if there is a better way to approach it? Or is Java just broken in this particular respect?</p>
<p>P.S. This is not the only thing that went wrong with the old application today. I upgraded from Java 5 to Java 6 and suddenly some XML was not compliant against a schema according to Java &#8211; I had hit <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6529766">this error</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/java-always-explicitly-specify-which-xml-parser-to-use/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Never close PHP class files with the &#8220;?&gt;&#8221; tag</title>
		<link>http://www.databasesandlife.com/never-close-php-class-files-with-the-tag/</link>
		<comments>http://www.databasesandlife.com/never-close-php-class-files-with-the-tag/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 09:47:38 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=348</guid>
		<description><![CDATA[When developing PHP, a front-end PHP file will include other files: classes,  utilities, etc.
When writing those class files, one also needs to use the &#60;?php tag at the start of the file, otherwise PHP will simply take the text and output it unchanged to the browser. (PHP&#8217;s assumption is that it sits in a [...]]]></description>
			<content:encoded><![CDATA[<p>When developing PHP, a front-end PHP file will include other files: classes,  utilities, etc.</p>
<p>When writing those class files, one also needs to use the <strong>&lt;?php</strong> tag at the start of the file, otherwise PHP will simply take the text and output it unchanged to the browser. (PHP&#8217;s assumption is that it sits in a web page, with probably more markup than code, so by default characters in the source code get copied one-to-one to the browser and the <strong>&lt;?php</strong>&#8230;<strong>?&gt;</strong> tags are necessary to introduce PHP to the &#8220;exceptional circumstance&#8221; that one might actually want to program some PHP.)</p>
<p>If one must open the class source file with <strong>&lt;?php</strong> then it would seem to make aesthetic sense to close it with <strong>?&gt;</strong>. However, there are no negative  side-effects if one does not close the tag, plus one very negative side-effect  if one does close it.</p>
<p>We performed a minor release a while ago, after which the display of generated PDF files no longer worked. Yet the minor release had nothing to do with the section of code  that produced PDFs. What sort of weird <a href="http://en.wikipedia.org/wiki/Action_at_a_distance_(computer_science)">action-at-a-distance</a> could possibly be happening here?</p>
<p>The reason was that one class file in the minor release had a blank line after the <strong>?&gt;</strong> tag. This was impossible to spot in the text editor. The blank line was printed to the browser, which was also invisible in nearly all of the site, as HTML ignores blank lines. PDFs probably do as well (I haven&#8217;t checked) but the problem wasn&#8217;t with the content. As HTTP response content is streamed to the browser (as opposed to being collected first and then sent to the browser at the end of the request), HTTP headers can only be set before the first byte of output has been produced by the software. As the blank line in the class source file consituted  content, and the source file was (necessarily) parsed before the code could be executed, the HTTP header &#8220;Content-Type: text/pdf&#8221; couldn&#8217;t be sent, and various errors about headers not being able to be sent, combined with the binary source  of the PDF, arrived at the user&#8217;s screen.</p>
<p>So given there  are no disadvantages, and one particulary weird source of bugs can be removed, I  would say one should never end PHP files with <strong>?&gt;</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/never-close-php-class-files-with-the-tag/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My favourite Hibernate error</title>
		<link>http://www.databasesandlife.com/my-favourite-hibernate-error/</link>
		<comments>http://www.databasesandlife.com/my-favourite-hibernate-error/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 13:35:41 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=345</guid>
		<description><![CDATA[&#8230; is this one. I&#8217;ve wasted many an hour searching for the cause of this. And it&#8217;s one you&#8217;re likely to run into pretty quickly when you try to write your first Hibernate configuration file.
The XML
&#60;one-to-many type=&#8221;OtherClass&#8221;/&#62;
delivers the error
Error parsing XML: Attribute &#8220;type&#8221; must be declared for element type &#8220;one-to-many&#8221;.
This looks like a perfectly self-explanatory [...]]]></description>
			<content:encoded><![CDATA[<p>&#8230; is this one. I&#8217;ve wasted many an hour searching for the cause of this. And it&#8217;s one you&#8217;re likely to run into pretty quickly when you try to write your first Hibernate configuration file.</p>
<p>The XML</p>
<blockquote><p>&lt;one-to-many type=&#8221;OtherClass&#8221;/&gt;</p></blockquote>
<p>delivers the error</p>
<blockquote><p>Error parsing XML: Attribute &#8220;type&#8221; must be declared for element type &#8220;one-to-many&#8221;.</p></blockquote>
<p>This looks like a perfectly self-explanatory error, however looking at the file, the element <em>does </em>have a &#8220;type&#8221; attribute. What should one do?</p>
<p>Thinking about it, I only just introduced the &#8220;type&#8221; attribute to the &lt;one-to-many&gt; element in my config. What happens if I change the attribute name to &#8220;fsdjkfdk&#8221;?</p>
<blockquote><p>&lt;one-to-many fsdjkfdk=&#8221;OtherClass&#8221;/&gt;</p></blockquote>
<p>The error is now:</p>
<blockquote><p>Error parsing XML: Attribute &#8220;fsdjkfdk&#8221; must be declared for element type &#8220;one-to-many&#8221;.</p></blockquote>
<p>What the error means is that the attribute <strong>must not </strong>be declared, as opposed to <strong>must</strong>.</p>
<p>It&#8217;s amusing to read even people on the Hibernate team get <a href="https://forum.hibernate.org/viewtopic.php?f=1&#038;t=946756&#038;start=0">confused</a> by this error, and can&#8217;t find a solution.</p>
<p><em>(Hibernate 3.3.1 &#8211; the most current version &#8211; although I encountered this error within the first hour of ever using Hibernate in Q1/2006.)</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/my-favourite-hibernate-error/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Per-CPU performance statistics are useless</title>
		<link>http://www.databasesandlife.com/per-cpu-performance-statistics-are-useless/</link>
		<comments>http://www.databasesandlife.com/per-cpu-performance-statistics-are-useless/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 11:06:17 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=334</guid>
		<description><![CDATA[Windows, Linux and OS X offer the ability to view the utilization of each CPU/core in the system. This is completely useless. On all these operating systems, tasks get switched from one core to another on a regular basis. (I don&#8217;t know why this happens, but I suppose there is no reason for it not [...]]]></description>
			<content:encoded><![CDATA[<p>Windows, Linux and OS X offer the ability to view the utilization of each CPU/core in the system. This is completely useless. On all these operating systems, tasks get switched from one core to another on a regular basis. (I don&#8217;t know why this happens, but I suppose there is no reason for it not to happen.)</p>
<p>Here is my <strong>CPU-bound single-threaded</strong> program running on a dual-core computer.</p>
<p><center><img src="http://www.databasesandlife.com/blog-attachments/20090401-task-mangaer-individual-cores.png" width=317 height=118></center></p>
<p>I suppose all one can really say is that if one has N cores and the average CPU% usage (over all cores) is approximately 100/N then probably one is running a program which can&#8217;t take advantage of multiple cores.</p>
<p>I would rather replace the current &#8220;CPU usage history, per core&#8221; multiple graphs with:</p>
<ul>
<li>One graph, showing a history of the average over all CPUs (visually the same as if one had a 1-core CPU).</li>
<li>I would then add horizontal marker lines: If one had 4 cores, I would add 4 equally spaced marker lines. This would show that if the performance reached a marker line (e.g. 25% for the first line) then probably running the equivalent of 1 single-threaded program.</li>
</ul>
<p>I mean it&#8217;s not a brilliant solution but I reckon it would be more meaningful than the way the information is currently displayed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/per-cpu-performance-statistics-are-useless/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Automatic reconnect from Hibernate to MySQL</title>
		<link>http://www.databasesandlife.com/automatic-reconnect-from-hibernate-to-mysql/</link>
		<comments>http://www.databasesandlife.com/automatic-reconnect-from-hibernate-to-mysql/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 11:44:34 +0000</pubDate>
		<dc:creator>adrian</dc:creator>
				<category><![CDATA[Broken]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.databasesandlife.com/?p=315</guid>
		<description><![CDATA[Yesterday I spent the entire day getting the following amazing state-of-the-art not-ever-done-before feature to work:

Executing a SQL statement from my program

Because, as everyone knows, I don&#8217;t suffer from NIHS, I used standard object-relational mapping software Hibernate, with a standard programming language Java, using the standard web-application server Tomcat, and now I am using the standard &#8220;connection pooling&#8221; software C3P0 (which [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I spent the entire day getting the following amazing state-of-the-art not-ever-done-before feature to work:</p>
<ul>
<li>Executing a SQL statement from my program</li>
</ul>
<p>Because, as everyone knows, I don&#8217;t suffer from <a href="http://www.databasesandlife.com/not-invented-here-syndrome/">NIHS</a>, I used standard object-relational mapping software <a href="http://www.hibernate.org/">Hibernate</a>, with a standard programming language Java, using the standard web-application server Tomcat, and now I am using the standard &#8220;connection pooling&#8221; software <a href="http://www.mchange.com/projects/c3p0/index.html">C3P0</a> (which I didn&#8217;t know I needed to execute a SQL statement, see below..)</p>
<p>The program is, in fact, already completed, and is nearly deployed. On the test server it works fine and even on the (future) live server it worked fine. But the customer noticed that if one installed it one day, the next day it didn&#8217;t work. I&#8217;ve had such symptoms many times before, so I know immediately what was going on:</p>
<ul class=tight>
<li>MySQL drops a connection after 8 hours (configurable)</li>
<li>The software is used during the day, but isn&#8217;t used during the night, therefore the connection times out in the night</li>
<li>Therefore in the morning, the program one installed the day before no longer works</li>
</ul>
<p>Perhaps I exaggerated the simplicity above of what I was really trying to achieve. It should really be expressed as the following:</p>
<ul>
<li>Executing a SQL statement from my program, even if a long time has passed since the last one was executed</li>
</ul>
<p>But that amounts to the same thing in my opinion! It isn&#8217;t rocket science! (But in fact is, see below..)</p>
<p>A obvious non-solution is to increase the &#8220;connection drop after&#8221; time on the MySQL server from 8 hours to e.g. &#8220;2 weeks&#8221; (&#8220;wait_timeout&#8221; in &#8220;mysql.cnf&#8221;). But software has got to be capable of reconnecting after a connection drops. The database server may need to be reset, it may crash, it may suffer hardware failure, etc. If, every time one restarts one particular service, one has to restart a thousand dependent services (maybe some Java, some Perl, some PHP, some robots, ..) and then maybe restart services which are dependent on them &#8211; that&#8217;s a maintenance nightmare. So the software has to be altered to be able to handle connection drops automatically, by reconnecting. Once the software has been so altered, one no longer needs to alter the &#8220;wait_timeout&#8221; on the server.</p>
<p>The error was:</p>
<blockquote><p>
org.hibernate.util.JDBCExceptionReporter: The last packet successfully received from the server was 56697 seconds ago. The last packet sent successfully to the server was 56697 seconds ago, which  is longer than the server configured value of &#8216;wait_timeout&#8217;. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property &#8216;autoReconnect=true&#8217; to avoid this problem.
</p></blockquote>
<p>Quite a helpful error message, don&#8217;t you think? But</p>
<ul class=tight>
<li>I&#8217;m not going to increase &#8220;wait_timeout&#8221; as discussed above,</li>
<li>&#8220;testing validity&#8221; in the application &#8211; well I was using standard software Hibernate which should take care of this sort of thing automatically, but evidently wasn&#8217;t</li>
<li>and we were already using ?autoReconnect=true in the JDBC URL (this evidently wasn&#8217;t working).</li>
</ul>
<p>I figured I really needed to get to the bottom of this. Googling just showed (many) people with the same problem, but no solutions. The only way to get to the bottom of software is to read the source. (It has been the way to resolve issues of simple things simply not working in MySQL <a href="http://www.databasesandlife.com/reading-row-by-row-into-java-from-mysql/">before</a>.)</p>
<p>I stopped looking in the MySQL source for why &#8220;autoReconnect=true&#8221; didn&#8217;t work when I saw the following text in the source describing the autoReconnect parameter:</p>
<blockquote><p>The use of this feature is not recommended, because it has side effects related to session state and data consistency
</p></blockquote>
<p>I have no idea what particular side-effects are meant here? I guess that&#8217;s left as an exercise for the reader, to test their imagination.</p>
<p>And anyway, I figure that a reconnect-facility belongs in the &#8220;application&#8221; (Hibernate in my case) as opposed to in database-vendor specific code. I mean the exactly the same logic would be necessary if one were connecting to PostgreSQL or Oracle, so it doesn&#8217;t make sense to build it in to the database driver.</p>
<p>So then I looked in the Hibernate code. To cut a long story short, the basic connection mechanism of Hibernate (as specified in all the introductory books and websites, which is probably how most people learn Hibernate) doesn&#8217;t support reconnecting, one has to use H3C0 connection pool (which itself <a href="http://forum.hibernate.org/viewtopic.php?t=934779">didn&#8217;t always support reconnecting</a>)</p>
<p>(I don&#8217;t want to use container/Tomcat-managed connections, as I have some command-line robots which do some work, and I don&#8217;t want to use different code for the robots as the web application. Although another company defined Servlets which did &#8220;robot work&#8221;, and the robot was just a &#8220;wget&#8221; entered into Tomcat &#8211; to get the user of container-managed connections &#8211; but this seems a too-complex solution to my taste..</p>
<p>But once one&#8217;s used H3C0, the default behavior seems to be that to process a request, if the connection is dead then the user sees and error &#8211; but at least it reconnects for the next request. I suppose one error is better than infinite errors, but still not as good as zero errors. It turns out one needs the option <a href="http://www.mchange.com/projects/c3p0/index.html#testConnectionOnCheckout">testConnectionOnCheckout</a> - which the documentation doesn&#8217;t recommend because testing the connection before a request might lead to lower performance. Surely the software firstly has to work, only secondly does it have to work fast.</p>
<p>So, to summarize, to get a connection to &#8220;work&#8221; (which I define as including handling dropped connections by reconnecting without error): In &#8220;hibernate.cfg.xml&#8221;:</p>
<pre>
&lt;!-- hibernate.cfg.xml --&gt;
&lt;property name="c3p0.min_size"&gt;5&lt;/property&gt;
&lt;property name="c3p0.max_size"&gt;20&lt;/property&gt;
&lt;property name="c3p0.timeout"&gt;1800&lt;/property&gt;
&lt;property name="c3p0.max_statements"&gt;50&lt;/property&gt;
&lt;property name="connection.provider_class"&gt;
   org.hibernate.connection.C3P0ConnectionProvider&lt;/property&gt;
&lt;!-- no "connection.pool_size" entry! --&gt;
</pre>
<p>Then create a file &#8220;c3p0.properties&#8221; which must be in the root of the classpath (i.e. no way to override it for particular parts of the application):</p>
<pre>
# c3p0.properties
c3p0.testConnectionOnCheckout=true
</pre>
<p>Amazing, that that stuff doesn&#8217;t just work out of the box. Programming the solution myself in Uboot took, I think, 1 line, and I&#8217;m sure it&#8217;s not more in <a href="http://max.xaok.org/webtek">WebTek</a> either.</p>
<p>That was an amazing amount of effort and research to get the simplest thing to work. Now if only this project had been paid by the hour&#8230;..</p>
<p><strong>[Update 28 May 2009] </strong>More Java hate today. Starting a new application, deployed it, and it didn&#8217;t work. In the morning, the application was down. Reason: The new project used Hibernate 3.3, and upgrade from 3.2 to 3.3 requires the &#8220;connection.provider_class&#8221; property to be set. Previously the presence of &#8220;c3p0.max_size&#8221; was enough.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.databasesandlife.com/automatic-reconnect-from-hibernate-to-mysql/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
	</channel>
</rss>
