tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andy Gumbrecht <>
Subject Dynamic datasources
Date Wed, 14 Oct 2009 08:37:32 GMT
Hi all,

Just thought I should share this snippet for creating DataSource 
resources either before or after an embedded OpenEJB has started.
Thanks go out to David Blevins for pointing me in the right direction.

The method can be fed a non-null environment (env) properties which will 
be seeded before use in the InitialContext call.
If the 'env' parameter is null then it is assumed that an InitialContext 
has been created for OpenEJB, in which case a live Resource is created.

Obviously this is quite specific to my requirements, but can be easily 

private static final Log log = LogFactory.getLog(Session.class);
private static final Set<String> _dataSources = new HashSet<String>();

      * Creates a DataSource resource for OpenEJB.
      * @param env If not null the properties are seeds with parameters 
to create the DataSource .
      * @param managed Is this a JTA managed or unmanaged DataSource.
      * @param f File location for the database - Is specific for file 
based databases such as Derby, etc.
      * @param name DataSource name - Will prepend to either 
[name]Managed or [name]Unmanaged.
      * @throws Exception If anything goes wrong.
private static void addDataSourceConfig(Properties env, final boolean 
managed, final File f, final String name) throws Exception {

         final String m = (managed ? "Managed" : "Unmanaged");

         if (_dataSources.contains(String.format("%1$s%2$s", name, m))) {
             //Already configured for OpenEJB

         final Resource resource = (null == env ? new 
Resource(String.format("%1$s%2$s", name, m), "DataSource") : null);
         final String dsname;

         if (null != resource) {
             env = resource.getProperties();
             dsname = "";
         } else {
             env.put(String.format("%1$s%2$s", name, m), 
             dsname = String.format("%1$s%2$s.", name, m);

         env.put(String.format("%1$sJtaManaged", dsname), (managed ? 
"true" : "false"));
         env.put(String.format("%1$sUserName", dsname), "SA");
         env.put(String.format("%1$sJdbcDriver", dsname), 
         env.put(String.format("%1$sJdbcUrl", dsname), 
f.getPath().replace("\\", "/"), m.toLowerCase()));

         if (null != resource) {

             try {
ConfigurationFactory().configureService(resource, ResourceInfo.class));
             } catch (OpenEJBException e) {
                 log.error("error:", e);
                 throw new Exception(String.format("Failed to add 
DataSource %1$s%2$s", name, m), e);

         _dataSources.add(String.format("%1$s%2$s", name, m));

Best regards,


PS. Something I also do which might be useful to someone is to create 
and load an application dynamically after OpenEJB has started.

//Dynamically load application
  SystemInstance.get().getComponent(Assembler.class).createApplication(new ConfigurationFactory().configureApplication(
     new File("my.ejb.jar")

View raw message