|
1 | 1 | == Java DataBase Connectivity |
2 | 2 |
|
| 3 | +=== The database in the layered architecture |
| 4 | + |
| 5 | +image:DatabaseInLayeredArchitecture.png[width=50%, title="Database in layered architecture"] Database in layered architecture. |
| 6 | + |
| 7 | +As you can see in the image above, to connect to a database managment system from your code you need a driver, which is a piece of software that implements the JDBC API and translates the calls to the database specific protocol. The driver is provided by the database vendor, or by a third party. You need to include the driver in your project as a dependency, so that it is available at runtime. |
| 8 | + |
| 9 | +When you are developing in a team, you will typically have different environments for development, testing and production. Each environment will have its own database instance, with its own credentials. You need to configure your application to use the correct database for each environment. In the development environment, each developer would like to use his own database instance, so that they can work independently and not interfere with each other. In the testing environment, database contents should be predictable, so that tests can be reliable. In the production environment, the database should be secure and performant. |
| 10 | + |
| 11 | +To avoid situations in which a database functionality does work for one developer ("Works-for-me!"), but not for another, it is good practice to work with a (Postgres) Docker container to make sure that all developers use the same database version and configuration. |
| 12 | + |
| 13 | +Sometimes it is handy to directly connect to your database using the PostgreSQL Command Line Client. You can do this on the terminal of your host machine (or on the terminal of your Docker container) by simply entering the command "psql". By default, psql will try to connect to a database with the same name as your username, using the same username and no password. If you have set up your database differently, you can specify the connection parameters in the command line. For example, if you want to connect with a specific user to a specific database, e.g. user 'Alex' with database 'studentDB', you can use the following command: psql -U Alex -d studentDB. You will be prompted to enter the password for user 'Alex'. If you run psql on the terminal of your docker container, it will by default use root as user, which will not work since that is not a default user in postgres. Therefore, in that case you should write 'psql -U postgres' to login with the postgres user (remember, this is a very powerfull user, so be careful with it!). |
| 14 | + |
| 15 | +Within the command line client, you can easily get insights into your database, for example by listing all tables with the command "\dt", or by describing a table with the command "\d tablename". with "\q" you can leave the client application again. Valuable is also "\h" to get an overview of all possible commands, or "\h DELETE" to learn everything about the syntax of the DELETE command, or "\h <command>" more general to get help. |
| 16 | + |
| 17 | +So far about the command line client. Dont forget the "Never evers" that we discussed in class: |
| 18 | +* Never write SQL queries in another layer than the Persistence layer. |
| 19 | +* Never use the "postgres" user in your application, because that user has too much privileges. Create a specific user for your application with only the necessary privileges. Be careful using the "postgres" user in general. |
| 20 | +* Never write user names and passwords hard-coded in your source code, because that is a security risk and also would make it difficult to change the credentials when needed. Use configuration files or environment variables to store the credentials securely and separately from the source code. |
| 21 | +* Never push database property files with credentials to your repository. Make sure to exclude such files from version control. Having a proprty file template with dummy credentials is fine, but make sure to exclude the actual file with real credentials from version control. |
| 22 | +* Never throw SQLException objects from your persistence layer to the business layer. Catch them in the persistence layer and throw a custom exception that is more meaningful for the business layer, such as PersistenceException. This way you can hide the implementation details of the database. |
3 | 23 |
|
4 | 24 | === DataBase connection credentials and Java Properties |
5 | 25 |
|
|
0 commit comments