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