<?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>Source Allies Blog &#187; Database</title>
	<atom:link href="http://blogs.sourceallies.com/category/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.sourceallies.com</link>
	<description>Technical and process thinking from Source Allies employees</description>
	<lastBuildDate>Mon, 06 Feb 2012 17:40:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Hibernate Date vs Timestamp</title>
		<link>http://blogs.sourceallies.com/2012/02/hibernate-date-vs-timestamp/</link>
		<comments>http://blogs.sourceallies.com/2012/02/hibernate-date-vs-timestamp/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 20:24:21 +0000</pubDate>
		<dc:creator>David Kessler</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[equals]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[timestamp]]></category>
		<category><![CDATA[UserType]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=2559</guid>
		<description><![CDATA[I encountered a subtle hibernate mapping issue involving Dates and Timestamps.  The following test recreates this issue.


package com.sourceallies.logging;
&#160;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
&#160;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
&#160;
import javax.annotation.Resource;
&#160;
import org.apache.commons.lang.time.DateUtils;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
&#160;
import com.sourceallies.Person;
&#160;
@RunWith&#40;SpringJUnit4ClassRunner.class&#41;
@ContextConfiguration
@TransactionConfiguration&#40;defaultRollback=false&#41;
public class HibernateDateTimeTest &#123;
&#160;
	@Resource
	private SessionFactory sessionFactory;
&#160;
	private Session currentSession;
&#160;
	private Date birthDateWithTime;
	private Date birthDateWithoutTime;
	private Date createDate;
	private [...]]]></description>
			<content:encoded><![CDATA[<p>I encountered a subtle hibernate mapping issue involving <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Date.html">Dates</a> and <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html">Timestamps</a>.  The following test recreates this issue.<br />
<span id="more-2559"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.logging</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertEquals</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertFalse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertTrue</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Timestamp</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Calendar</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.annotation.Resource</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.lang.time.DateUtils</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.Criteria</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.SessionFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.classic.Session</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Before</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Test</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.runner.RunWith</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.ContextConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.junit4.SpringJUnit4ClassRunner</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.transaction.TransactionConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.transaction.annotation.Transactional</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sourceallies.Person</span><span style="color: #339933;">;</span>
&nbsp;
@RunWith<span style="color: #009900;">&#40;</span>SpringJUnit4ClassRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
@ContextConfiguration
@TransactionConfiguration<span style="color: #009900;">&#40;</span>defaultRollback<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HibernateDateTimeTest <span style="color: #009900;">&#123;</span>
&nbsp;
	@Resource
	<span style="color: #000000; font-weight: bold;">private</span> SessionFactory sessionFactory<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Session currentSession<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDateWithTime<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDateWithoutTime<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> createDate<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> modifyDate<span style="color: #339933;">;</span>
&nbsp;
	@Before
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span><span style="color: #009900;">&#123;</span>
		currentSession <span style="color: #339933;">=</span> sessionFactory.<span style="color: #006633;">getCurrentSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		birthDateWithTime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		birthDateWithoutTime <span style="color: #339933;">=</span> DateUtils.<span style="color: #006633;">truncate</span><span style="color: #009900;">&#40;</span>birthDateWithTime, <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">DATE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createDate <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		modifyDate <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSave<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		Person person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testFirst&quot;</span>, <span style="color: #0000ff;">&quot;testLast&quot;</span>, 
                      birthDateWithTime, createDate, modifyDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		saveOrUpdate<span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testFind<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, people.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Person foundPerson <span style="color: #339933;">=</span> people.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>birthDateWithTime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>birthDateWithoutTime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>createDate<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>modifyDate<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> saveOrUpdate<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		currentSession.<span style="color: #006633;">saveOrUpdate</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		currentSession.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Criteria criteria <span style="color: #339933;">=</span> currentSession.<span style="color: #006633;">createCriteria</span><span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> criteria.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> people<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here is the mapping for person.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">...
@<span style="color: #003399;">Entity</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
&nbsp;
	@Id
	@GeneratedValue<span style="color: #009900;">&#40;</span>strategy<span style="color: #339933;">=</span>GenerationType.<span style="color: #006633;">AUTO</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>1L<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> firstName<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lastName<span style="color: #339933;">;</span>
&nbsp;
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;timestamp&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> createDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> modifyDate<span style="color: #339933;">;</span>
	...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There are three <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Date.html">Date</a> fields: &#8216;birthdate&#8217;, &#8216;createDate&#8217;, and &#8216;modifyDate&#8217;.  The fields &#8216;birthDate&#8217; and &#8216;createDate&#8217; have a specified <a href="http://docs.jboss.org/hibernate/core/3.5/javadoc/org/hibernate/annotations/Type.html">Type</a>.  The field &#8216;modifyDate&#8217; however, does not have a specified Type.  The generated SQL from Hibernate is as follows.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> Person <span style="color: #66cc66;">&#40;</span>id bigint generated <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">AS</span> identity <span style="color: #66cc66;">&#40;</span>start <span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
birthDate date<span style="color: #66cc66;">,</span> createDate timestamp<span style="color: #66cc66;">,</span> firstName varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
lastName varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> modifyDate timestamp<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>We see that Hibernate created &#8216;birthDate&#8217; as a &#8216;Date&#8217; and the other two were created as a &#8216;Timestamp&#8217;.  The primary difference between &#8216;Date&#8217; and &#8216;Timestamp&#8217; in SQL is that &#8216;Timestamp&#8217; holds the date and time while the &#8216;Date&#8217; only holds the date.</p>
<p>At first glance this is not a big deal, but let&#8217;s take a closer look at the test.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSave<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		Person person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testFirst&quot;</span>, <span style="color: #0000ff;">&quot;testLast&quot;</span>, 
                           birthDateWithTime, createDate, modifyDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		saveOrUpdate<span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Person is created with three <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Date.html">Dates</a> that all include date and time values.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testFind<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, people.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Person foundPerson <span style="color: #339933;">=</span> people.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>birthDateWithTime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>birthDateWithoutTime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>createDate<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>modifyDate<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When Hibernate retrieves a Person from the database the &#8216;createDate&#8217; and &#8216;modifyDate&#8217; have been converted to <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html">Timestamps</a>.  At first glance this doesn&#8217;t appear to be a big deal.  <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html">java.sql.Timestamp</a> extends <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Date.html">java.util.Date</a>.  But why are they not equal.  I found a helpful answer to this question in <a href="http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683">Effective Java (2nd Edition)</a> page 41.</p>
<p><em><br />
There are some classes in the Java platform libraries that do extend an instantiable<br />
class and add a value component. For example, java.sql.Timestamp<br />
extends java.util.Date and adds a nanoseconds field. The equals implementation<br />
for Timestamp does violate symmetry and can cause erratic behavior if<br />
Timestamp and Date objects are used in the same collection or are otherwise intermixed.<br />
The Timestamp class has a disclaimer cautioning programmers against<br />
mixing dates and timestamps. While you won’t get into trouble as long as you<br />
keep them separate, there’s nothing to prevent you from mixing them, and the<br />
resulting errors can be hard to debug. This behavior of the Timestamp class was a<br />
mistake and should not be emulated. (Bloch, Effective Java, 2nd Ed.)<br />
</em></p>
<p>If you want to read more about &#8216;equals&#8217; read chapter 3, Item 8 in <a href="http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683">Effective Java (2nd Edition)</a>.  Apart from a hack there are two primary ways to solve this issue.  The first way involves typing &#8216;createDate&#8217; and &#8216;modifyDate&#8217; to <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html">Timestamp</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	...
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Timestamp</span> createDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Timestamp</span> modifyDate<span style="color: #339933;">;</span>
        ...</pre></div></div>

<p>The second approach uses a custom <a href="http://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/usertype/UserType.html">UserType</a>.  Here is a custom UserType that converts Timestamp to Date and vise versa.  There is a side effect with this solution.  Converting Timestamp to Date drops the nanosecond precision.  If this is not acceptable then the previous solution should be used.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Serializable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.PreparedStatement</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.ResultSet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.SQLException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Timestamp</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Types</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.HibernateException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.usertype.UserType</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DateTimeUserType  <span style="color: #000000; font-weight: bold;">implements</span> UserType <span style="color: #009900;">&#123;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> sqlTypes<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		 <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #003399;">Types</span>.<span style="color: #006633;">TIMESTAMP</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;rawtypes&quot;</span><span style="color: #009900;">&#41;</span>
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">Class</span> returnedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">Date</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> x, <span style="color: #003399;">Object</span> y<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		 <span style="color: #000000; font-weight: bold;">return</span> x <span style="color: #339933;">==</span> y <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> y <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> x.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> hashCode<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> x<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> x.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> nullSafeGet<span style="color: #009900;">&#40;</span><span style="color: #003399;">ResultSet</span> rs, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> names, <span style="color: #003399;">Object</span> owner<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">throws</span> HibernateException, <span style="color: #003399;">SQLException</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Timestamp</span> timestamp <span style="color: #339933;">=</span> rs.<span style="color: #006633;">getTimestamp</span><span style="color: #009900;">&#40;</span>names<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>rs.<span style="color: #006633;">wasNull</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span>timestamp.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> nullSafeSet<span style="color: #009900;">&#40;</span><span style="color: #003399;">PreparedStatement</span> st, <span style="color: #003399;">Object</span> value, <span style="color: #000066; font-weight: bold;">int</span> index<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">throws</span> HibernateException, <span style="color: #003399;">SQLException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>value <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            st.<span style="color: #006633;">setNull</span><span style="color: #009900;">&#40;</span>index, <span style="color: #003399;">Types</span>.<span style="color: #006633;">TIMESTAMP</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Date</span> date <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Date</span><span style="color: #009900;">&#41;</span> value<span style="color: #339933;">;</span>
            <span style="color: #003399;">Timestamp</span> timestamp <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#40;</span>date.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            st.<span style="color: #006633;">setTimestamp</span><span style="color: #009900;">&#40;</span>index, timestamp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> deepCopy<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> value<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> value<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isMutable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Serializable</span> disassemble<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> value<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		 <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Serializable</span><span style="color: #009900;">&#41;</span> value<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> assemble<span style="color: #009900;">&#40;</span><span style="color: #003399;">Serializable</span> cached, <span style="color: #003399;">Object</span> owner<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> cached<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> replace<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> original, <span style="color: #003399;">Object</span> target, <span style="color: #003399;">Object</span> owner<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> original<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here is the way to use it in the Person class.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@<span style="color: #003399;">Entity</span>
@TypeDefs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		@TypeDef<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dateTimeUserType&quot;</span>, typeClass<span style="color: #339933;">=</span>DateTimeUserType.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
	...
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dateTimeUserType&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> createDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dateTimeUserType&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> modifyDate<span style="color: #339933;">;</span>
        ...</pre></div></div>

<p>Either of these solutions will prevent the &#8216;equals&#8217; issue that we encountered before.  This issue is not a Hibernate issue it is truly a Java issue as is stated in <a href="http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683">Effective Java (2nd Edition)</a>.  Hibernate can be challenging enough without these subtle issues that are easy to miss until a bug is reported.</p>
<p>Here is the complete code. <a href="http://blogs.sourceallies.com/wp-content/uploads/2012/02/HibernateDateTime3.zip" > Hibernate Logging Zip </a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2012/02/hibernate-date-vs-timestamp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Incrementing Oracle Sequences Without Permissions</title>
		<link>http://blogs.sourceallies.com/2010/04/incrementing-oracle-sequences-without-permissions/</link>
		<comments>http://blogs.sourceallies.com/2010/04/incrementing-oracle-sequences-without-permissions/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 19:06:52 +0000</pubDate>
		<dc:creator>Tim Bierbaum</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[oracle]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=1499</guid>
		<description><![CDATA[If you are ever working with a database where data is consistently being imported, dropped, and/or restored; sooner or later you will find yourself in the situation where the primary key of a table is no longer synchronized with the sequence used to generate it.

If you have the appropriate permissions, this is easy to fix. [...]]]></description>
			<content:encoded><![CDATA[<p>If you are ever working with a database where data is consistently being imported, dropped, and/or restored; sooner or later you will find yourself in the situation where the primary key of a table is no longer synchronized with the sequence used to generate it.<br />
<span id="more-1499"></span><br />
If you have the appropriate permissions, this is easy to fix.  Say you need to advance the sequence by 500, you just execute the following sql:</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;"><span style="color: #00F;">ALTER</span> sequence MY_SEQ increment <span style="color: #00F;">BY</span> <span style="color: #800;">500</span><span style="color: #00F;">;</span>
<span style="color: #00F;">SELECT</span> MY_SEQ<span style="color: #00F;">.</span><span style="color: #00F;">NEXTVAL</span> <span style="color: #00F;">FROM</span> dual<span style="color: #00F;">;</span>
<span style="color: #00F;">ALTER</span> sequence MY_SEQ increment <span style="color: #00F;">BY</span> <span style="color: #800;">1</span><span style="color: #00F;">;</span></pre></div></div>

<p>What do you do if you don&#8217;t have the correct permissions to do this?  You can always call up the dba and ask him.  If you&#8217;re impatient, you can also do a little pl/sql programming to accomplish the same thing.  All you want to do is just repeatedly select the next value until the sequence value is higher that the current key value.</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;"><span style="color: #00F;">DECLARE</span>
  current_value <span style="color: #00F;">NUMBER</span><span style="color: #00F;">;</span>
  seq_val <span style="color: #00F;">NUMBER</span><span style="color: #00F;">;</span>
<span style="color: #00F;">BEGIN</span>
  <span style="color: #00F;">SELECT</span> <span style="color: #000;">MAX</span><span style="color: #00F;">&#40;</span>MY_TABLE<span style="color: #00F;">.</span>id<span style="color: #00F;">&#41;</span> <span style="color: #00F;">INTO</span> current_val <span style="color: #00F;">FROM</span> MY_TABLE<span style="color: #00F;">;</span>
  <span style="color: #00F;">WHILE</span> current_val <span style="color: #00F;">&gt;=</span> seq_val
  <span style="color: #00F;">LOOP</span>
    <span style="color: #00F;">SELECT</span> MY_SEQ<span style="color: #00F;">.</span><span style="color: #00F;">NEXTVAL</span> <span style="color: #00F;">INTO</span> seq_val <span style="color: #00F;">FROM</span> dual<span style="color: #00F;">;</span>
  <span style="color: #00F;">END</span> <span style="color: #00F;">LOOP</span><span style="color: #00F;">;</span>
<span style="color: #00F;">END</span><span style="color: #00F;">;</span></pre></div></div>

<p>Of course, if you encounter this situation once, chances are that you will encounter it many times.  Next, you can create a function that takes the table name, key column name, and sequence name and then executes dynamically created sql to fix the problem.</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;"><span style="color: #00F;">CREATE</span> <span style="color: #00F;">OR</span> <span style="color: #000;">REPLACE</span> <span style="color: #00F;">PROCEDURE</span> INCREMENT_SEQ<span style="color: #00F;">&#40;</span><span style="color: #00F;">IN</span> TABLE_NAME<span style="color: #00F;">,</span> <span style="color: #00F;">IN</span> ID_COLUMN_NAME<span style="color: #00F;">,</span> <span style="color: #00F;">IN</span> SEQUENCE_NAME<span style="color: #00F;">&#41;</span> <span style="color: #00F;">IS</span>
<span style="color: #00F;">DECLARE</span>
  current_value <span style="color: #00F;">NUMBER</span><span style="color: #00F;">;</span>
  seq_val <span style="color: #00F;">NUMBER</span> <span style="color: #00F;">:=</span> <span style="color: #00F;">-</span><span style="color: #800;">1</span><span style="color: #00F;">;</span>
<span style="color: #00F;">BEGIN</span>
  <span style="color: #00F;">EXECUTE</span> <span style="color: #00F;">IMMEDIATE</span> <span style="color: #F00;">'select max('</span> <span style="color: #00F;">||</span> TABLE_NAME <span style="color: #00F;">||</span> <span style="color: #F00;">'.'</span> <span style="color: #00F;">||</span> ID_COLUMN_NAME <span style="color: #00F;">||</span> <span style="color: #F00;">') into current value from '</span> <span style="color: #00F;">||</span> TABLE_NAME<span style="color: #00F;">;</span>
  <span style="color: #00F;">WHILE</span> current_val <span style="color: #00F;">&gt;=</span> seq_val
  <span style="color: #00F;">LOOP</span>
    <span style="color: #00F;">EXECUTE</span> <span style="color: #00F;">IMMEDIATE</span> <span style="color: #F00;">'select '</span> <span style="color: #00F;">||</span> SEQUENCE_NAME <span style="color: #00F;">||</span> <span style="color: #F00;">&quot;.nextval into seq_val from dual';
  end loop;
END INCREMENT_SEQ;</span></pre></div></div>

<p>Now, you should be able to fix any sequence/key mismatches simply by calling the procedure.</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;">INCREMENT_SEQ<span style="color: #00F;">&#40;</span><span style="color: #F00;">'MY_TABLE'</span><span style="color: #00F;">,</span> <span style="color: #F00;">'ID'</span><span style="color: #00F;">,</span> <span style="color: #F00;">'MY_SEQ'</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/04/incrementing-oracle-sequences-without-permissions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySQL Master/Slave configuration with EJB3 and JPA</title>
		<link>http://blogs.sourceallies.com/2010/03/mysql-masterslave-configuration-with-ejb3-and-jpa/</link>
		<comments>http://blogs.sourceallies.com/2010/03/mysql-masterslave-configuration-with-ejb3-and-jpa/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 18:33:19 +0000</pubDate>
		<dc:creator>Vaithi  Subramanian</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Mysql JPA replication]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=1398</guid>
		<description><![CDATA[Well this turned out to be quite an exercise.
The goal: scalable reads with MySQL in master-slave configuration, writing to the master, and reading from N slaves, load balanced in round-robin fashion (or something).
The problem: using JPA (Java Persistence API) instead of direct JDBC calls. Turns out the MySQL ReplicationDriver (used to load balance reads to [...]]]></description>
			<content:encoded><![CDATA[<p>Well this turned out to be quite an exercise.</p>
<p>The goal: scalable reads with MySQL in master-slave configuration, writing to the master, and reading from N slaves, load balanced in round-robin fashion (or something).</p>
<p>The problem: using JPA (Java Persistence API) instead of direct JDBC calls. Turns out the MySQL ReplicationDriver (used to load balance reads to slaves and send writes to the master) relies on the readOnly state of the Connection in order to decide whether it&#8217;s a read or a write. With direct JDBC calls, I could get the Connection and toggle the readOnly state as needed.<br />
<span id="more-1398"></span><br />
However, buried under JPA and EntityManager and so on, there&#8217;s no way to do that. So I looked into other solutions (LBPool, for instance), but nothing out there seems to be  intended for JPA environment.</p>
<p>So I had to do it myself&#8230; in digging around in the MySQL Connector/J source, I discovered the loadbalance option, intended for use w/ MySQL Cluster, because in that environment, you don&#8217;t need to distinguish between master and slave, they&#8217;re all just nodes.</p>
<p>The loadbalance option wouldn&#8217;t work directly for me, of course, because I don&#8217;t want to load balance writes, just reads.</p>
<p>Soulution to the problem is to use two data sources, one for reads, and the other for writes, I can stick the read data source behind all my &#8220;fetch stuff&#8221; APIs, and use the write data source for everything else. Then I can use the loadbalance option for the read data source, because I know for certain I&#8217;m never sending it any writes.</p>
<p>I am currently using Jboss 4.2.1 GA here my mysql-ds.xml</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;datasources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;no-tx-datasource<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jndi-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MasterA<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jndi-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;connection-url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>jdbc:mysql://masterDB/databasename<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/connection-url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;driver-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.mysql.jdbc.ReplicationDriver<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/driver-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;user-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>username<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/user-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;password<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>password<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/password<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;min-pool-size<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>10<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/min-pool-size<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- Don't set this any higher than max_connections on your</span>
<span style="color: #808080; font-style: italic;">         MySQL server, usually this should be a 10 or a few 10's</span>
<span style="color: #808080; font-style: italic;">         of connections, not hundreds or thousands --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;max-pool-size<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>50<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/max-pool-size<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;idle-timeout-minutes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>10<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/idle-timeout-minutes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;exception-sorter-class-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.mysql.jdbc.integration.jboss.ExtendedMysqlExceptionSorter<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/exception-sorter-class-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;valid-connection-checker-class-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/valid-connection-checker-class-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;metadata<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mySQL<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/metadata<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/no-tx-datasource<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;no-tx-datasource<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jndi-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SlaveA<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jndi-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;connection-url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>jdbc:mysql://slaveA,slaveB/databasename<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/connection-url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;driver-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.mysql.jdbc.ReplicationDriver<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/driver-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;user-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>username<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/user-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;password<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>password<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/password<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;min-pool-size<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>10<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/min-pool-size<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- Don't set this any higher than max_connections on your</span>
<span style="color: #808080; font-style: italic;">         MySQL server, usually this should be a 10 or a few 10's</span>
<span style="color: #808080; font-style: italic;">         of connections, not hundreds or thousands --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;max-pool-size<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>50<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/max-pool-size<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;idle-timeout-minutes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>10<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/idle-timeout-minutes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;exception-sorter-class-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.mysql.jdbc.integration.jboss.ExtendedMysqlExceptionSorter<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/exception-sorter-class-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;valid-connection-checker-class-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/valid-connection-checker-class-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;metadata<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>mySQL<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/metadata<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/no-tx-datasource<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/datasources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Persistence.xml for the JTA datasources.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;persistence</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/persistence&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/persistence persistence_1_0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;persistence-unit</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Master&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;provider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.hibernate.ejb.HibernatePersistence<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/provider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jta-data-source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		java:/MasterA
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jta-data-source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.dialect&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;org.hibernate.dialect.MySQL5Dialect&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>			
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.connection.readOnly&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> 	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/persistence-unit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;persistence-unit</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Slave&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;provider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.hibernate.ejb.HibernatePersistence<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/provider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jta-data-source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		java:/SlaveA
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jta-data-source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.dialect&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;org.hibernate.dialect.MySQL5Dialect&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>			
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.connection.readOnly&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> 	
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/persistence-unit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/persistence<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>For normal JDBC connections read only state can changed like the following code.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
    ReplicationDriver driver <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ReplicationDriver<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003399;">Properties</span> props <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Properties</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// We want this for failover on the slaves</span>
    props.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;autoReconnect&quot;</span>, <span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// We want to load balance between the slaves</span>
    props.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;roundRobinLoadBalance&quot;</span>, <span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    props.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user&quot;</span>, <span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    props.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;password&quot;</span>, <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//</span>
    <span style="color: #666666; font-style: italic;">// Looks like a normal MySQL JDBC url, with a</span>
    <span style="color: #666666; font-style: italic;">// comma-separated list of hosts, the first</span>
    <span style="color: #666666; font-style: italic;">// being the 'master', the rest being any number</span>
    <span style="color: #666666; font-style: italic;">// of slaves that the driver will load balance against</span>
    <span style="color: #666666; font-style: italic;">//</span>
&nbsp;
    <span style="color: #003399;">Connection</span> conn <span style="color: #339933;">=</span>
        driver.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jdbc:mysql:replication://master,slave1,slave2,slave3/databasename&quot;</span>,
            props<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//</span>
    <span style="color: #666666; font-style: italic;">// Perform read/write work on the master</span>
    <span style="color: #666666; font-style: italic;">// by setting the read-only flag to &quot;false&quot;</span>
    <span style="color: #666666; font-style: italic;">//</span>
&nbsp;
    conn.<span style="color: #006633;">setReadOnly</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    conn.<span style="color: #006633;">setAutoCommit</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    conn.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPDATE some_table ....&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    conn.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//</span>
    <span style="color: #666666; font-style: italic;">// Now, do a query from a slave, the driver automatically picks one</span>
    <span style="color: #666666; font-style: italic;">// from the list</span>
    <span style="color: #666666; font-style: italic;">//</span>
&nbsp;
    conn.<span style="color: #006633;">setReadOnly</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003399;">ResultSet</span> rs <span style="color: #339933;">=</span>
      conn.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT a,b FROM alt_table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
     .......
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Conculsion:<br />
Thus with the help of two data sources I am able to replicate the functionality of Master/Slave Read scalability. If you are using the normal JDBC connection then changing the read-only is as simple as the above code. Hope this will be helpful for someone who needs to setup Replication in the code level.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/03/mysql-masterslave-configuration-with-ejb3-and-jpa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IP Addresses in PHP/MySQL</title>
		<link>http://blogs.sourceallies.com/2010/02/ip-addresses-in-phpmysql/</link>
		<comments>http://blogs.sourceallies.com/2010/02/ip-addresses-in-phpmysql/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 13:32:39 +0000</pubDate>
		<dc:creator>Mark Ciecior</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=924</guid>
		<description><![CDATA[I&#8217;ve been working on a web-based tool that stores, among other network-related things, IP addresses.  When I first started I stored each IP address as four TINYINTS (0-255 for each octet):

mysql&#62; DESC ipaddresses;
+----------+---------------------+------+-----+---------+----------------+
&#124; FIELD    &#124; Type                [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a web-based tool that stores, among other network-related things, IP addresses.  When I first started I stored each IP address as four TINYINTS (0-255 for each octet):</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">DESC</span> ipaddresses;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------+---------------------+------+-----+---------+----------------+</span>
<span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">FIELD</span>    <span style="color: #66cc66;">|</span> Type                <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #66cc66;">|</span> Extra          <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------+---------------------+------+-----+---------+----------------+</span>
<span style="color: #66cc66;">|</span> id       <span style="color: #66cc66;">|</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span>    <span style="color: #66cc66;">|</span> NO   <span style="color: #66cc66;">|</span> PRI <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span>    <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> A        <span style="color: #66cc66;">|</span> tinyint<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #66cc66;">|</span> NO   <span style="color: #66cc66;">|</span>     <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span>    <span style="color: #66cc66;">|</span>                <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> B        <span style="color: #66cc66;">|</span> tinyint<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #66cc66;">|</span> NO   <span style="color: #66cc66;">|</span>     <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span>    <span style="color: #66cc66;">|</span>                <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> C        <span style="color: #66cc66;">|</span> tinyint<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #66cc66;">|</span> NO   <span style="color: #66cc66;">|</span>     <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span>    <span style="color: #66cc66;">|</span>                <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> D        <span style="color: #66cc66;">|</span> tinyint<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #66cc66;">|</span> NO   <span style="color: #66cc66;">|</span>     <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span>    <span style="color: #66cc66;">|</span>                <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------+---------------------+------+-----+---------+----------------+</span>
<span style="color: #cc66cc;">5</span> rows <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> ipaddresses <span style="color: #993333; font-weight: bold;">WHERE</span> id<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+----+----+----+-----+</span>
<span style="color: #66cc66;">|</span> id <span style="color: #66cc66;">|</span> A  <span style="color: #66cc66;">|</span> B  <span style="color: #66cc66;">|</span> C  <span style="color: #66cc66;">|</span> D   <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+----+----+----+-----+</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">10</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">20</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">30</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">131</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+----+----+----+-----+</span>
<span style="color: #cc66cc;">1</span> row <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.02</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>As I started manipulating these addresses I found it awkward to do common binary math (like bitwise ANDs).  I decided instead to store these 32-bit values as unsigned integers (of length 32).  To make my life easier yet, MySQL and PHP both have native functions to convert IP addresses between my old and new formats to make this migration extremely easy.<br />
<span id="more-924"></span><br />
In MySQL:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> INET_ATON<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'10.20.30.131'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---------------------------+</span>
<span style="color: #66cc66;">|</span> INET_ATON<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'10.20.30.131'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---------------------------+</span>
<span style="color: #66cc66;">|</span>                 <span style="color: #cc66cc;">169090691</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---------------------------+</span>
<span style="color: #cc66cc;">1</span> row <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> INET_NTOA<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">169090691</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------------------+</span>
<span style="color: #66cc66;">|</span> INET_NTOA<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">169090691</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------------------+</span>
<span style="color: #66cc66;">|</span> 10<span style="color: #66cc66;">.</span>20<span style="color: #66cc66;">.</span>30<span style="color: #66cc66;">.</span>131         <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------------------+</span>
<span style="color: #cc66cc;">1</span> row <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>In PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ip_string</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;10.20.30.131&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ip_integer</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ip2long</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ipstring</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$ip_integer</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">## 169090691
</span><span style="color: #b1b100;">echo</span> <span style="color: #990000;">long2ip</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip_integer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">## 10.20.30.131</span></pre></div></div>

<p>In another example of inefficiency, I used to gather all sorts of info from the user and use PHP to make multiple SQL queries before modifying my data.  I&#8217;ve since learned about stored procedures &#8211; the database does all the work and I only need to make one SQL call from PHP.  This presented me with an unexpected problem, however:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">mysql<span style="color: #66cc66;">&gt;</span> CALL getAddress<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+----+----+----+----+</span>
<span style="color: #66cc66;">|</span> id <span style="color: #66cc66;">|</span> A  <span style="color: #66cc66;">|</span> B  <span style="color: #66cc66;">|</span> C  <span style="color: #66cc66;">|</span> D  <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+----+----+----+----+</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">10</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">20</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">30</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">131</span><span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+----+----+----+----+</span>
<span style="color: #cc66cc;">1</span> row <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span> rows affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The string &#8220;Query OK, 0 rows affected (0.00 sec)&#8221; is a second result set, which the PHP <i>mysql</i> library can&#8217;t handle.  The fix here is to use the <i>mysqli</i> library instead and access only the result sets you need.  In my case I&#8217;m only concerned with the first result set so I added a bunch of wasteful while loops after each of my CALL queries.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysqli_next_result</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Lastly, PHP&#8217;s ip2long function treats 32-bit numbers as signed ints, so I had to specifically treat them as unsigned ints:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">echo $ip_integer; <span style="color: #808080; font-style: italic;">## BAD</span>
echo sprintf<span style="color: #66cc66;">&#40;</span>%u<span style="color: #66cc66;">,</span> $ip_integer<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">## GOOD</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/02/ip-addresses-in-phpmysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building Multi-Criteria Search Queries in Hibernate</title>
		<link>http://blogs.sourceallies.com/2009/12/building-multi-criteria-search-queries-in-hibernate/</link>
		<comments>http://blogs.sourceallies.com/2009/12/building-multi-criteria-search-queries-in-hibernate/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 18:02:53 +0000</pubDate>
		<dc:creator>Vaithi  Subramanian</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[Hql]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=399</guid>
		<description><![CDATA[
In this post I am going to show how to write queries multi-criteria search screens. There are two approaches for making this possible.


HQL for building the Query
Building Query using Criteria API

HQL for building the Query
Here I am going to show 2 approaches to building the HQL and try to point out the better approach.
Approach I:String [...]]]></description>
			<content:encoded><![CDATA[<p>
In this post I am going to show how to write queries multi-criteria search screens. There are two approaches for making this possible.
</p>
<ul>
<li>HQL for building the Query</li>
<li>Building Query using Criteria API</li>
</ul>
<p><b>HQL for building the Query</b></p>
<p>Here I am going to show 2 approaches to building the HQL and try to point out the better approach.</p>
<p><strong>Approach I:String concatenation</strong><br />
This approach uses String concatenation and setting up the values directly in the query.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>startDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>firstClause<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      query <span style="color: #339933;">=</span> query <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; where &quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      query <span style="color: #339933;">=</span> query <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; and &quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   query <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot; s.date &gt;= '&quot;</span> <span style="color: #339933;">+</span> startDate <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Using the above approach  there might be a chance of SQL Injection attack and using string concatenation is inherently error-prone.</p>
<p><strong>Approach 2: Criteria as Named Parameters</strong></p>
<p>In this one we create two map to hold parameter name and value which could be binded to the HQL during the execution.</p>
<p>Here is brief example of the approach.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">List</span> search<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   StringBuilder aQuery <span style="color: #339933;">=</span> 
      <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; from document p where p.id is not null &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003399;">HashMap</span> parameterMap <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003399;">HashMap</span> parameterListMap <span style="color: #339933;">=</span>  <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//Collection Criteria</span>
   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>countyList <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      buildCollectionCriterion<span style="color: #009900;">&#40;</span>aQuery, parameterListMap, <span style="color: #0000ff;">&quot;listID&quot;</span>, countyList<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//Date Criteria</span>
   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>startDate<span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> endDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      buildDateCriterion<span style="color: #009900;">&#40;</span>aQuery, parameterMap, <span style="color: #0000ff;">&quot;dateField&quot;</span>,startDate, endDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   Query query <span style="color: #339933;">=</span> hibSession.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>aQuery.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key <span style="color: #339933;">:</span> parameterMap.<span style="color: #006633;">keySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      query.<span style="color: #006633;">setParameter</span><span style="color: #009900;">&#40;</span>key, parameterMap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key <span style="color: #339933;">:</span> parameterListMap.<span style="color: #006633;">keySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      query.<span style="color: #006633;">setParameterList</span><span style="color: #009900;">&#40;</span>key, parameterListMap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #003399;">List</span> results <span style="color: #339933;">=</span> query.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Helper Methods for different type of criteria</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> buildCollectionCriterion<span style="color: #009900;">&#40;</span>StringBuilder aQuery, 
                                        <span style="color: #003399;">Map</span> parameterListMap, 
                                        <span style="color: #003399;">String</span> aFieldName, 
                                        <span style="color: #003399;">Collection</span> aList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>aList <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>aList.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      aQuery
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; and p.&quot;</span><span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>aFieldName<span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; in (:&quot;</span><span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>aFieldName<span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      parameterListMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>aFieldName, aList<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> buildDateCriterion<span style="color: #009900;">&#40;</span>StringBuilder aQuery, 
                               <span style="color: #003399;">Map</span> parameterMap, 
                               <span style="color: #003399;">String</span> aFieldName, 
                               <span style="color: #003399;">Date</span> aStartDate, 
                               <span style="color: #003399;">Date</span> anEndDate<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>aStartDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> anEndDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      aQuery
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; and ( p.&quot;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>aFieldName<span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; between :aStartDate and :anEndDate)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      parameterMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;aStartDate&quot;</span>, aStartDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      parameterMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;anEndDate&quot;</span>, anEndDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>aStartDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      aQuery
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; and  (p.&quot;</span><span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>aFieldName<span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &gt;= :aStartDate)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      parameterMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;aStartDate&quot;</span>, aStartDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span> 
   <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>anEndDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      aQuery
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; and (p.&quot;</span><span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>aFieldName<span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &lt;=:anEndDate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      parameterMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;anEndDate&quot;</span>, anEndDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><b>Building Query using Criteria API</b></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">List</span> search<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Criteria c <span style="color: #339933;">=</span> hibSession.<span style="color: #006633;">createCriteria</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Document</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        c.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>Restrictions.<span style="color: #006633;">notNull</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>countryList <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                c.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>Restrictions.<span style="color: #006633;">in</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;listId&quot;</span>, countryList<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>startDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                c.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>Restrictions.<span style="color: #006633;">ge</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dateField&quot;</span>, startDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>endDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                c.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>Restrictions.<span style="color: #006633;">le</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dateField&quot;</span>, endDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> c.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Conclusion</strong></p>
<p>The Hibernate Criteria API is a powerful and elegant library which is well adapted for implementing multi-criteria search functionality and also HQL queries must be built &#8216;on-the-fly&#8217;. Using it in appropriate circumstances will result in cleaner, clearer, more reliable and more maintainable code. </p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2009/12/building-multi-criteria-search-queries-in-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PL/SQL Variables and Connection Pooling</title>
		<link>http://blogs.sourceallies.com/2009/11/plsql-variables-and-connection-pooling/</link>
		<comments>http://blogs.sourceallies.com/2009/11/plsql-variables-and-connection-pooling/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 16:56:44 +0000</pubDate>
		<dc:creator>Tim Bierbaum</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[lessons]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[pl/sql]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=413</guid>
		<description><![CDATA[I recently had to implement a common feature across multiple applications and app servers, all of which point to the same Oracle database.  For reasons unrelated, I chose to implement this feature using PL/SQL.  You can all stop laughing now.  I ended up with something resembling:

CREATE OR REPLACE PACKAGE BODY MY_PKG IS
 [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to implement a common feature across multiple applications and app servers, all of which point to the same Oracle database.  For reasons unrelated, I chose to implement this feature using PL/SQL.  You can all stop laughing now.  I ended up with something resembling:</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;"><span style="color: #00F;">CREATE</span> <span style="color: #00F;">OR</span> <span style="color: #000;">REPLACE</span> <span style="color: #00F;">PACKAGE</span> <span style="color: #00F;">BODY</span> MY_PKG <span style="color: #00F;">IS</span>
   g_enabled <span style="color: #00F;">BOOLEAN</span> <span style="color: #00F;">:=</span> <span style="color: #00F;">TRUE</span><span style="color: #00F;">;</span>
&nbsp;
   <span style="color: #00F;">PROCEDURE</span> enable_sp<span style="color: #00F;">&#40;</span><span style="color: #00F;">&#41;</span> <span style="color: #00F;">IS</span>
      <span style="color: #00F;">BEGIN</span>
         g_enabled <span style="color: #00F;">:=</span> <span style="color: #00F;">TRUE</span><span style="color: #00F;">;</span>
      <span style="color: #00F;">END</span><span style="color: #00F;">;</span>
&nbsp;
   <span style="color: #00F;">PROCEDURE</span> disable_sp<span style="color: #00F;">&#40;</span><span style="color: #00F;">&#41;</span> <span style="color: #00F;">IS</span>
      <span style="color: #00F;">BEGIN</span>
         g_enabled <span style="color: #00F;">:=</span> <span style="color: #00F;">FALSE</span><span style="color: #00F;">;</span>
      <span style="color: #00F;">END</span><span style="color: #00F;">;</span>
&nbsp;
   <span style="color: #00F;">FUNCTION</span> is_enabled_fn<span style="color: #00F;">&#40;</span><span style="color: #00F;">&#41;</span> <span style="color: #00F;">IS</span>
      l_return_value <span style="color: #00F;">NUMBER</span><span style="color: #00F;">;</span>
&nbsp;
      <span style="color: #00F;">BEGIN</span>
         <span style="color: #00F;">IF</span> g_enabled <span style="color: #00F;">THEN</span>
            l_return_value <span style="color: #00F;">:=</span> <span style="color: #800;">1</span><span style="color: #00F;">;</span>
         <span style="color: #00F;">ELSE</span>
            l_return_value <span style="color: #00F;">:=</span> <span style="color: #800;">0</span><span style="color: #00F;">;</span>
         <span style="color: #00F;">END</span> <span style="color: #00F;">IF</span><span style="color: #00F;">;</span>
&nbsp;
         <span style="color: #00F;">RETURN</span> l_return_value<span style="color: #00F;">;</span>
      <span style="color: #00F;">END</span><span style="color: #00F;">;</span>
<span style="color: #00F;">END</span> MY_PKG<span style="color: #00F;">;</span></pre></div></div>

<p>Seems pretty innocent, doesn&#8217;t it?</p>
<p>So, I write unit tests in java to test everything.  It all seems to be working.  It goes through code review.  Nothing more serious than cosmetic issues were found.  It goes through the qa cycles.  It all seems to be working.  It gets deployed to production.  At first, it seems to be working.  Then I start to see some interesting behavior.</p>
<h3>Lesson #1:</h3>
<p>PL/SQL data types aren&#8217;t as straight forward as I would have liked.  The variable g_enabled is of type BOOLEAN.  This is a data type built into PL/SQL.  It is not a data type in Oracle&#8217;s version of SQL.  Why is this significant?  Consider the two following scenario&#8217;s and their result (assuming that is_enabled=TRUE) -</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;"><span style="color: #00F;">BEGIN</span>
&nbsp;
  <span style="color: #00F;">DBMS_OUTPUT</span><span style="color: #00F;">.</span>put_line<span style="color: #00F;">&#40;</span>my_pkg<span style="color: #00F;">.</span>is_enabled_fn<span style="color: #00F;">&#40;</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">;</span>
&nbsp;
<span style="color: #00F;">END</span><span style="color: #00F;">;</span></pre></div></div>

<p>This will print out a 1 to the standard output.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> my_pkg<span style="color: #66cc66;">.</span>is_enabled_fn<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> dual;</pre></div></div>

<p>This query will return 0.</p>
<p>This was not what I expected.  I would have expected the second query to return a 1.  The reason it doesn&#8217;t, is that the query is not run in a PL/SQL block.  This results in the BOOLEAN data type to not be defined and the if statement in is_enabled_fn() to always evaluate to false.</p>
<h3>Lesson #2:</h3>
<p>Package variables are connection private.  I create two connections, A and B, to the database.  If I call disable_sp() in connection A, is_enabled_fn() will still return a 1 in connection B.  The value of the variable is not shared across the connections.  If I were then to create a connection C, is_enabled_fn() will return 0.  This is because connection C was created after the value was set in connection A. While it wasn&#8217;t what I initially expected, it makes some sense in that the connections don&#8217;t share memory.  The really big problem comes into play when app servers pool connections and might not close them for weeks at a time.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2009/11/plsql-variables-and-connection-pooling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

