[jboss-user] [Datasource Configuration] - Re: How do you programmatically create datasources in JBoss5

Tom Coupland do-not-reply at jboss.com
Thu Sep 1 07:43:57 EDT 2011


Tom Coupland [http://community.jboss.org/people/tcoup] created the discussion

"Re: How do you programmatically create datasources in JBoss5"

To view the discussion, visit: http://community.jboss.org/message/624249#624249

--------------------------------------------------------------
Following 'fans xnet's excellant advise i did create a factory. I even (eventually) worked out what to put in it!

In order to build an EMF you need to do something like:

{code}
 public EntityManagerFactory buildEntityManagerFactory(DataSource datasource) { 
        Ejb3Configuration ejbconf = new Ejb3Configuration();

        ejbconf.setDataSource(datasource);
        ejbconf.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
        ejbconf.setProperty("hibernate.show_sql", "false");

        //Make transactionally aware
        ejbconf.setProperty("hibernate.transaction.manager_lookup_class", "org.hibernate.transaction.JBossTransactionManagerLookup");
        ejbconf.setProperty("jta.UserTransaction", "java:comp/UserTransaction");
        ejbconf.setProperty("hibernate.current_session_context_class", "jta");
        ejbconf.setProperty(HibernatePersistence.TRANSACTION_TYPE, PersistenceUnitTransactionType.JTA.name());

        //Add caching
        ejbconf.setProperty("hibernate.cache.provider_class", "HibernateEhCacheProviderClass");
        ejbconf.setProperty("hibernate.cache.use_second_level_cache", "true");
        ejbconf.setProperty("net.sf.ehcache.configurationResourceName", "META-INF/ehcache.xml");

        //Add you entity beans here
        ejbconf.addAnnotatedClass(SomeEntityBean.class);

        return  ejbconf.buildEntityManagerFactory();
{code}

Knocking up a datasource with DBCP looks like:

{code} 
    public DataSource buildDatasource(String connectionUrl) {
        // Create base connection factory that produces raw db connections.
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionUrl, libraryUser,
                libraryPassword);

        // Object pool for pooling raw connections
        GenericObjectPool connPool = new GenericObjectPool();
        connPool.setMaxActive(maxShardConns);
        connPool.setMinIdle(minShardConns);
        connPool.setMaxIdle(maxIdleConns);
        connPool.setMinEvictableIdleTimeMillis(shardConnTimeOutMins * 60 * 1000);
        connPool.setTestOnBorrow(true);

        connPool = new InstrumentedObjectPool(extractServerName(connectionUrl), connPool);

        String connectionValidationSql = "SELECT 1;";
        KeyedObjectPoolFactory preparedStatementPool = null;
        Boolean defaultConnectionsToReadOnly = false;
        Boolean defaultConnectionsToAutoCommit = false;

        TransactionManager transactionManager;
        try {
            transactionManager = (TransactionManager) InitialContext.doLookup("java:/TransactionManager");
        } catch (NamingException e) {
            throw new RuntimeException("Unable to lookup transaction manager", e);
        }

        LocalXAConnectionFactory transactionFactory = new LocalXAConnectionFactory(transactionManager,
                connectionFactory);

        // Create factory to manage the connectionPool
        PoolableManagedConnectionFactory poolableConnectionFactory = new PoolableManagedConnectionFactory(
                transactionFactory, connPool, preparedStatementPool, connectionValidationSql,
                defaultConnectionsToReadOnly, defaultConnectionsToAutoCommit);

        ManagedDataSource dataSource = new ManagedDataSource(connPool, transactionFactory.getTransactionRegistry());

        return dataSource;
{code}

Using these in combination will give you a transactionally aware EMF with connection pooling.
--------------------------------------------------------------

Reply to this message by going to Community
[http://community.jboss.org/message/624249#624249]

Start a new discussion in Datasource Configuration at Community
[http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2077]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20110901/b69b44f1/attachment.html 


More information about the jboss-user mailing list