Configure a default data source

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

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

To access a database from the application, configure a data source.


Steps

  1. Configure the datasource element with the ID DefaultDataSource in server.xml.

      <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");


Parent topic: Configure relational database connectivity in Liberty