Monday, 7 July 2014

URI vs. URL vs. URN

What's a URI?


Uniform Resource Identifiers (URIs) are used to identify 'names' or 'resources'. They come in 2 varieties: URNs and URLs. In fact, a URI can be both an URN and an URL!

What's a URL?


Uniform Resource Locators (URLs) provide a way to locate a resource using a specific scheme, most often but not limited to HTTP. Just think of a URL as an address to a resource, and the scheme as a specification of how to get there.

What's a URN?


Uniform Resource Names are identifiers for resources. They are location independent and make use of the urn: scheme.

What's the syntax of a URI?


scheme:scheme-specific-part?query#fragment

Examples from the RFC:
ftp://ftp.is.co.za/rfc/rfc1808.txt
http://www.ietf.org/rfc/rfc2396.txt
ldap://[2001:db8::7]/c=GB?objectClass?one
news:comp.infosystems.www.servers.unix
tel:+1-816-555-1212
telnet://192.0.2.16:80/
urn:oasis:names:specification:docbook:dtd:xml:4.1.2

What's the syntax of a URL?


scheme://username:password@subdomain.domain.tld:port/path/file-name.suffix?query-string#hash

Examples:
http://www.google.com
http://foo:bar@w1.superman.com/very/long/path.html?p1=v1&p2=v2#more-details
https://secured.com:443
ftp://ftp.bogus.com/~some/path/to/a/file.txt

What's the syntax of a URN?


urn:namespame-identifier:namespace-specific-string

Examples from Wikipedia:
urn:isbn:0451450523
urn:ietf:rfc:2648
urn:uuid:6e8bc430-9c3a-11d9-9669-0800200c9a66

What's an XML Namespace?

<ui:composition
        contentType="text/html"
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:a4j="http://richfaces.org/a4j"
        xmlns:rich="http://richfaces.org/rich"
        xmlns:s="http://jboss.org/schema/seam/taglib"
        xmlns:git="http://www.gouwit.nl/jsf/corepresentation"
        xmlns:gbl="http://www.gouwit.nl/jsf/bl
        xmlns:g="http://java.sun.com/jsf/composite/components
        xmlns:p="http://primefaces.org/ui
        template="/layout/template_globals.xhtml"
>

An XML Namespace name is a uniform resource identifier (URI). However, the namespace specification does not require nor suggest that the namespace URI be used to retrieve information; it is simply treated by an XML parser as a string.

In general users should assume that the namespace URI is simply a name, not the address of a document on the Web.

Updated: 2014-09-18, added XML Namespace note.

References

Freeformatter.com
http://www.freeformatter.com/url-parser-query-string-splitter.html
Stackoverflow - What is the difference between a uri and a url
http://stackoverflow.com/questions/176264/whats-the-difference-between-a-uri-and-a-url
IETF - RFC3986
http://www.ietf.org/rfc/rfc3986.txt
Wikipedia - XML namespace
http://en.wikipedia.org/wiki/XML_namespace

Monday, 30 June 2014

Hibernate and Caching

One of the major problems of debugging, is that sometimes there are bugs, but they only happen some of the time1.

This makes it hard to fix, unless you can spot the pattern required for the bug to appear.

The Problem

My colleague at work had such a one.

Sometimes the software threw an exception and complained that the table in the database did not actually exist. Sometimes the program provided the data required, and went merrily on its way.

It's crazy for a database table to exist only some of the time.

It made sense in this case that the table didn't exist. Because that table wasn't in the database. The Entity was purely used as a 'View' and several methods gathered the information to load an instance of the Entity.

The Explanation


Hibernate provides caching, meaning that a lookup by Identifier for an Entity returns the Entity from the Cache without hitting the database, if the Entity has been loaded already once.

Turns out our method, which loaded an Entity, totally didn't work at all! It just happened to return a Cached instance in some cases without problems.

A cached instance that was loaded by using another method that did not rely on findByIdentifier (for example via "select new Constructor()").

References

[1] The Kingdom of Transformation
http://www.csd.uwo.ca/~magi/personal/humour/Computer_Audience/The%20Kingdom%20of%20Transformation.html
[2] Hibernate - Object Identity
http://docs.jboss.org/hibernate/orm/4.3/devguide/en-US/html/ch02.html#d5e824