Configuring a default data source

You can configure a default data source that is associated with different JDBC providers for database connectivity. The JDBC providers supply the driver implementation classes that are required for JDBC connectivity with your specific vendor database.

Open Liberty For the most current information about configuring a default data source for Liberty, see the Open Liberty website

About this task

To access a database from your application, you must configure a data source.

Procedure

  1. Configure the datasource element with the ID DefaultDataSource in the server.xml file.
    <dataSource id="DefaultDataSource">
        <jdbcDriver libraryRef="MyJDBCLib"/>
        <properties.derby.embedded databaseName="myDB" createDatabase="create"/>
        <containerAuthData user="user1"  password="{xor}Oz0vKDtu" />
    </dataSource>
    <library id="MyJDBCLib">
        <file name="C:/derby/derby.jar"/>
    </library>
  2. To use the DefaultDataSource in a web application, a reference can be obtained with dependency injection:
    @Resource 
    DataSource defaultDataSource;
    or through JNDI lookup:
    DataSource defaultDataSource = (DataSource) new InitialContext().lookup("java:comp/DefaultDataSource");