Skip to content

Instantly share code, notes, and snippets.

@carlosbarragan
Created November 29, 2016 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosbarragan/13f5b47d7989e503db19e3f32ab41747 to your computer and use it in GitHub Desktop.
Save carlosbarragan/13f5b47d7989e503db19e3f32ab41747 to your computer and use it in GitHub Desktop.
Example for creating a DataSource via CDI
/*
* Please note for this to work you need to add the corresponding dependencies.
* In this case, we use https://github.com/brettwooldridge/HikariCP as connection pool
* and an embedded derby database.
*/
@Produces
public DataSource exposeDataSource() {
Properties props = new Properties();
props.setProperty("dataSource.databaseName", "mydb");
props.put("dataSource.logWriter", new PrintWriter(System.out));
HikariConfig config = new HikariConfig(props);
config.setJdbcUrl("jdbc:derby:memory:myDB;create=true");
return new HikariDataSource(config);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment