Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/opwvhk/jdbc-populator
https://github.com/opwvhk/jdbc-populator
Last synced: 9 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/opwvhk/jdbc-populator
- Owner: opwvhk
- License: apache-2.0
- Created: 2023-05-01T06:42:36.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-07-21T07:06:57.000Z (over 1 year ago)
- Last Synced: 2023-07-21T08:25:28.347Z (over 1 year ago)
- Language: Java
- Size: 126 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
JDBC Populator
==============Created by Oscar Westra van Holthe - Kind
Date: 2012-04-04The JDBC populator is a library with one core interface: `net.sf.opk.populator.JDBCPopulator`,
which in turn has a single method: `populateDatabase(java.sql.Connection)`. Its purpose is to populate a
database just before it is used. As such, it can help to build an Instant Developer Experience.In order to make this all work, the library also contains several implementations of this interface, als also
implementations of `javax.sql.DataSource` and `javax.sql.XADataSource`.Usage
-----To use the JDBC populator, add it as a dependency to your application server (or servlet container). Also, don't
forget to include a database. Using Maven and an embedded application server, you can do that like this:```
net.sf.opk
embedded-glassfish-web-plugin
1.3
${project.build.configDirectory}/glassfish-resources.xml
net.sf.opk
jdbc-populator
3.0
org.hsqldb
hsqldb
2.2.8
```
The file mentioned, `glassfish-resources.xml`, has content as explained in the next section.
Data Sources; GlassFish
-----------------------As an example says more than a long explanation, the following example is a `glassfish-resources.xml` file
that defines both an XA and a non-XA data source, both of which use a `net.sf.opk.populator.JDBCPopulator` to
fill their respective databases. All possible uses of a `net.sf.opk.populator.JDBCPopulator` are demonstrated.The same example (except the XA part) is also available for other containers (see below).
```
```
The `JDBCPopulator` is called upon first use of each data source.
This enables the creation of an _Instant Developer Experience_, as a datasource for an in-memory database can be
populated upon first use. This means that if you start the application, your application will have data available.Additionally, the `JDBCPopulator` can also populate our EJB component tests. Using JavaEE 6, you can use:
```
@DataSourceDefinition(name="java:global/MyApp/MyDataSource", ...)
public MyCodeTest
{
@Resource(name = "java:global/MyApp/MyDataSource")
DataSource datasource;@Before
public void populateDatabase() throws SQLException, IOException
{
try (Connection connection = datasource.getConnection())
{
JDBCPopulator populator = new net.sf.opk.populator.sql.FileSqlPopulator("/path/to/file.sql");
populator.populateDatabase(datasource.getConnection());
}
}
}
```Other JNDI Containers
---------------------In addition to GlassFish, JDBC Populator also supports other JNDI implementations. It has also been tested on
Tomcat 7.0 and Jetty 8.1. The JDBC Populator should also work (but has _not_ been tested) on JBoss, WebLogic,
WebSphere and Resin.The only containers that are specifically not supported are those that cannot create generic JavaBean resources in
JNDI without the help of a custom JNDI `ObjectFactory`.Jetty
-----For the example below, you also need a database connection pool implementation. The example below uses Commons DBCP.
Example `jetty-env.xml`:
```
org.hsqldb.jdbcDriver
jdbc:hsqldb:mem:TestDB
sa
5
5
5
src/main/config/database.sql
src/main/config/db-updates
jdbc/applicationDataSource
```
Tomcat
------Example `context.xml`:
```
```
Conclusion
----------The `JDBCPopulator` is a valuable asset during development, both for trying out an application and to test
components contacting a database.