<?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; Hibernate</title>
	<atom:link href="http://blogs.sourceallies.com/category/java/hibernate-java/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, 26 Jul 2010 15:01:47 +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>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>Hibernate Embeddable Objects</title>
		<link>http://blogs.sourceallies.com/2010/01/hibernate-embeddable-objects/</link>
		<comments>http://blogs.sourceallies.com/2010/01/hibernate-embeddable-objects/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 14:00:06 +0000</pubDate>
		<dc:creator>Max  Kuipers</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Embeddable Objects]]></category>
		<category><![CDATA[hibernate]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=579</guid>
		<description><![CDATA[Hibernate Embeddable Objects are a really neat way to organize your data model.  Especially, if you have the same few columns in a number of different tables, that all pertain to the same thing. The example commonly used is Addresses.  You may have a number of tables that each have fields pertaining to address information [...]]]></description>
			<content:encoded><![CDATA[<p>Hibernate Embeddable Objects are a really neat way to organize your data model.  Especially, if you have the same few columns in a number of different tables, that all pertain to the same thing. The <a title="example commonly used" href="http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e569">example commonly used </a>is Addresses.  You may have a number of tables that each have fields pertaining to address information and you don&#8217;t want to have to do all the mappings for each entity again and again.  If the column names are the same across each table, you can just add an @Embeddable annotation.<br />
<span id="more-579"></span><br />
Consider the following example:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Embeddable
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Address <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span> <span style="color: #009900;">&#123;</span>
...
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lineOne<span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lineTwo<span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> city<span style="color: #339933;">;</span>
....
   @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;LINEONE&quot;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getLineOne<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;">this</span>.<span style="color: #006633;">lineOne</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
...
   @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CITY&quot;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getCity<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;">this</span>.<span style="color: #006633;">city</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
...</pre></div></div>

<p>This creates address as an embeddable object. Then to actually embed it in your entity, you simply do something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@<span style="color: #003399;">Entity</span>
@Table<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Person&quot;</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: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span><span style="color: #009900;">&#123;</span>
...
   <span style="color: #000000; font-weight: bold;">private</span> Address address<span style="color: #339933;">;</span>
...
   @Embedded
   <span style="color: #000000; font-weight: bold;">public</span> getAddress<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;">this</span>.<span style="color: #006633;">address</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There is, however, one huge problem with this, especially when working with JSF.  If all of the fields in the embeddable object are null for some reason or another, then the embeddable object never actually gets instantiated.  There is a solution/hack that works around this &#8211; you modify the setter method to look something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@<span style="color: #003399;">Entity</span>
@Table<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Person&quot;</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: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span><span style="color: #009900;">&#123;</span>
...
   <span style="color: #000000; font-weight: bold;">private</span> Address address<span style="color: #339933;">;</span>
...
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setAddress<span style="color: #009900;">&#40;</span>Address address<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>address <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;">this</span>.<span style="color: #006633;">address</span> <span style="color: #339933;">=</span> address<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: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">address</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Address<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> 
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It&#8217;s not particularly pretty, but it will dry your tears in a pinch. More on this solution can be found <a title="here" href="https://jira.jboss.org/jira/browse/HIBERNATE-50">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/01/hibernate-embeddable-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
