<!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: How do you programmatically create datasources in JBoss5
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="http://community.jboss.org/people/tcoup">Tom Coupland</a> in <i>Datasource Configuration</i> - <a href="http://community.jboss.org/message/624249#624249">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Following 'fans xnet's excellant advise i did create a factory. I even (eventually) worked out what to put in it!</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>In order to build an EMF you need to do something like:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>{code}</p><p> public EntityManagerFactory buildEntityManagerFactory(DataSource datasource) { </p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; Ejb3Configuration ejbconf = new Ejb3Configuration();</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ejbconf.setDataSource(datasource);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ejbconf.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ejbconf.setProperty("hibernate.show_sql", "false");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; //Make transactionally aware</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ejbconf.setProperty("hibernate.transaction.manager_lookup_class", "org.hibernate.transaction.JBossTransactionManagerLookup");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ejbconf.setProperty("jta.UserTransaction", "java:comp/UserTransaction");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ejbconf.setProperty("hibernate.current_session_context_class", "jta");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ejbconf.setProperty(HibernatePersistence.TRANSACTION_TYPE, PersistenceUnitTransactionType.JTA.name());</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; //Add caching</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ejbconf.setProperty("hibernate.cache.provider_class", "HibernateEhCacheProviderClass");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ejbconf.setProperty("hibernate.cache.use_second_level_cache", "true");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ejbconf.setProperty("net.sf.ehcache.configurationResourceName", "META-INF/ehcache.xml");</p><p>&#160;&#160;&#160;&#160;&#160; </p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; //Add you entity beans here</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ejbconf.addAnnotatedClass(SomeEntityBean.class);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return&#160; ejbconf.buildEntityManagerFactory();</p><p>{code}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Knocking up a datasource with DBCP looks like:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>{code} </p><p>&#160;&#160;&#160; public DataSource buildDatasource(String connectionUrl) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; // Create base connection factory that produces raw db connections.</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionUrl, libraryUser,</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; libraryPassword);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; // Object pool for pooling raw connections</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; GenericObjectPool connPool = new GenericObjectPool();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; connPool.setMaxActive(maxShardConns);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; connPool.setMinIdle(minShardConns);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; connPool.setMaxIdle(maxIdleConns);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; connPool.setMinEvictableIdleTimeMillis(shardConnTimeOutMins * 60 * 1000);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; connPool.setTestOnBorrow(true);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; connPool = new InstrumentedObjectPool(extractServerName(connectionUrl), connPool);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; String connectionValidationSql = "SELECT 1;";</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; KeyedObjectPoolFactory preparedStatementPool = null;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; Boolean defaultConnectionsToReadOnly = false;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; Boolean defaultConnectionsToAutoCommit = false;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; TransactionManager transactionManager;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; try {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; transactionManager = (TransactionManager) InitialContext.doLookup("java:/TransactionManager");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; } catch (NamingException e) {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; throw new RuntimeException("Unable to lookup transaction manager", e);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; LocalXAConnectionFactory transactionFactory = new LocalXAConnectionFactory(transactionManager,</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; connectionFactory);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; // Create factory to manage the connectionPool</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; PoolableManagedConnectionFactory poolableConnectionFactory = new PoolableManagedConnectionFactory(</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; transactionFactory, connPool, preparedStatementPool, connectionValidationSql,</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; defaultConnectionsToReadOnly, defaultConnectionsToAutoCommit);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ManagedDataSource dataSource = new ManagedDataSource(connPool, transactionFactory.getTransactionRegistry());</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return dataSource;</p><p>{code}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Using these in combination will give you a transactionally aware EMF with connection pooling.</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/624249#624249">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in Datasource Configuration at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2077">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


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

</div>

</body>
</html>