<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Re: Dinamically switch DataSource in EJB3 application
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="http://community.jboss.org/people/viggo.navarsete">Viggo Navarsete</a> in <i>EJB3</i> - <a href="http://community.jboss.org/message/574235#574235">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Hi,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>if you know the JNDI names of the datasources that you want to switch between, you can do it like this:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>private Map&lt;String, EntityManager&gt; cachedEntityManagerMap = new HashMap&lt;String, EntityManager&gt;(); // cached EntityManagers</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">private EntityManager getEntityManager() {</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">String jndiName = "java:/something/someDS"; // TODO: Here you need to determine the JNDI name of the DataSource you want to switch to!</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">log.info( "Change datasource to '" + jndiName + "'" );</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"> </div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">if( cachedEntityManagerMap.containsKey( jndiName ) ) { // if already created, return a cached version instead of creating a new</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">log.debug( "Return cached EntityManager with jndiName '" + jndiName + "'" );</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">return cachedEntityManagerMap.get( jndiName );</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">} else {</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">log.debug( "Return new EntityManager with jndiName '" + jndiName + "'" );</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">/*</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">* See "Hibernate Entity Manager User Guide", chapter "Bootstrapping" for more examples of what to override in</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">* the map</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">*/</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Map&lt;String, String&gt; emMap = new HashMap&lt;String, String&gt;();</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">emMap.put( "javax.persistence.jtaDataSource", jndiName );</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">EntityManager em = Persistence.createEntityManagerFactory( "somePU", emMap )</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">.createEntityManager();</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">cachedEntityManagerMap.put( jndiName, em );</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">return em;</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 28px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div><p><span style="white-space: pre;"> </span>private EntityManager getEntityManager() {</p><p style="padding-left: 30px;"><span style="white-space: pre;"> </span>String jndiName = "java:/something/someDS"; // TODO: Here you need to determine the JNDI name of the DataSource you want to switch to!</p><p style="padding-left: 30px;"><span style="white-space: pre;"> </span>log.info( "Change datasource to '" + jndiName + "'" );</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="padding-left: 30px;"><span style="white-space: pre;"> </span>if( cachedEntityManagerMap.containsKey( jndiName ) ) { // if already created, return a cached version instead of creating a new</p><p style="padding-left: 60px;"><span style="white-space: pre;"> </span>log.debug( "Return cached EntityManager with jndiName '" + jndiName + "'" );</p><p style="padding-left: 60px;"><span style="white-space: pre;"> </span>return cachedEntityManagerMap.get( jndiName );</p><p style="padding-left: 30px;"><span style="white-space: pre;"> </span>} else {</p><p style="padding-left: 60px;"><span style="white-space: pre;"> </span>log.debug( "Return new EntityManager with jndiName '" + jndiName + "'" );</p><p style="padding-left: 60px;"><span style="white-space: pre;"> </span>/*</p><p style="padding-left: 60px;"><span style="white-space: pre;"> </span> * See "Hibernate Entity Manager User Guide", chapter "Bootstrapping" for more examples of what to override in</p><p style="padding-left: 60px;"><span style="white-space: pre;"> </span> * the map</p><p style="padding-left: 60px;"><span style="white-space: pre;"> </span> */</p><p style="padding-left: 60px;"><span style="white-space: pre;"> </span>Map&lt;String, String&gt; emMap = new HashMap&lt;String, String&gt;();</p><p style="padding-left: 60px;"><span style="white-space: pre;"> </span>emMap.put( "javax.persistence.jtaDataSource", jndiName );</p><p style="padding-left: 60px;"><span style="white-space: pre;"> </span>EntityManager em = Persistence.createEntityManagerFactory( "somePU", emMap )</p><p style="padding-left: 60px;"><span style="white-space: pre;"> </span>.createEntityManager();</p><p style="padding-left: 60px;"><span style="white-space: pre;"> </span>cachedEntityManagerMap.put( jndiName, em );</p><p style="padding-left: 60px;"><span style="white-space: pre;"> </span>return em;</p><p style="padding-left: 30px;"><span style="white-space: pre;"> </span>}</p><p><span style="white-space: pre;"> </span>}</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/574235#574235">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in EJB3 at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>