L.09 Flashcards

Transactions, JDBC and ORM

1
Q

What are functional dependencies?

A

A set of attributes X functionally determines a set of attributes Y if the value of X determines a unique value for Y. Mathematically: X → Y.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give an example of a functional dependency.

A

Social Security Number (SSN) determines an employee name: SSN → ENAME.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the conditions for an acceptable functional dependency?

A

It is acceptable if
(1) X or a subset of X is a key and X == Y,
or (2) X or a subset of X is a foreign key pointing at a key of Y.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the three normal forms?

A

1NF: No composed or multi-valued attributes.

2NF: Every non-key attribute is functionally dependent on the primary key.

3NF: No non-key attribute is functionally dependent on another non-key attribute.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a transaction in databases?

A

A transaction is an atomic set of statements that must either all be executed or none at all.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are transaction boundaries?

A

Transaction boundaries are defined using:
BEGIN TRANSACTION
COMMIT TRANSACTION
ROLLBACK TRANSACTION

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are ACID properties of transactions?

A

-Atomicity: All changes are applied, or none.
-Consistency: The database remains in a valid state.
-Isolation: Transactions do not interfere with each other.
-Durability: Committed changes persist even after failures.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are some transactional problems?

A

Lost updates: Two transactions update the same data, leading to a loss of one update.

Temporary updates: One transaction reads an uncommitted update.

Nonrepeatable reads: A transaction reads the same data twice but gets different values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the database isolation levels in PostgreSQL?

A

SERIALIZABLE

REPEATABLE READ

READ COMMITTED (default in PostgreSQL)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is JDBC?

A

JDBC (Java Database Connectivity) is a Java API that allows interaction with databases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the steps in a JDBC interaction?

A

1) Load the driver class.
2) Establish a connection.
3) Execute queries.
4) Process results.
5) Close the connection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Give an example of a JDBC connection string.

A

jdbc:postgresql://localhost:5432/company (for PostgreSQL)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a PreparedStatement in JDBC?

A

A precompiled SQL statement that allows parameterized queries, improving performance and security.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Object-Relational Mapping (ORM)?

A

ORM maps database tables to Java objects, making database interaction more object-oriented.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is JPA (Java Persistence API)?

A

JPA is a specification that automates ORM, allowing Java objects to be mapped to database tables using annotations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you define an entity in JPA?

A

@Entity
class Employee {
@Id private String ssn;
@NotNull private String lname;
private String fname;
}

17
Q

What are the advantages of using JPA?

A

Simplifies database interactions.

Reduces boilerplate SQL code.

Supports automatic entity mapping.

18
Q

What are the common ways to execute queries in JPA?

A

By ID ( em.find(EntityType, PrimaryKeyValue) ).

Using SQL ( em.createNativeQuery(query, EntityType) ).

Named queries ( em.createNamedQuery(“myQuery”) ).

Criteria API ( query by example ).

19
Q

What are the different inheritance mapping strategies in JPA?

A

Joined (table-per-subtype).

Table-Per-Class (table-per-type).

Single-Table (all data in one table).

20
Q

What is the key takeaway from Transactions, JDBC, and ORM?

A

Transactions ensure database consistency with ACID properties.

JDBC provides a low-level way to interact with databases using SQL.

JPA simplifies database interactions with ORM and entity mapping.

21
Q

What is connection pooling in JDBC?

A

Connection pooling is a technique that reuses database connections instead of opening and closing them repeatedly, improving performance.

22
Q

What is the difference between Statement and PreparedStatement in JDBC?

A

Statement: Used for simple queries, created every time.

PreparedStatement: Precompiled and reused, preventing SQL injection and improving performance.

23
Q

How does JDBC handle transactions?

A

JDBC transactions can be managed manually by setting auto-commit to false, executing statements, and committing or rolling back as needed.

24
Q

What is a ResultSet in JDBC?

A

A ResultSet is a linked list of rows returned from a query, iterated using a cursor ( e.g., rs.next() ).

25
What is SQL injection, and how can it be prevented?
SQL injection is an attack where malicious SQL is inserted into queries. It is prevented using PreparedStatements instead of concatenating strings.
26
What is the purpose of the merge() method in JPA?
The merge() method updates an existing entity in the database or inserts it if it does not exist.
27
What is the role of the EntityManager in JPA?
The EntityManager manages the persistence lifecycle, including creating, reading, updating, and deleting entities.
28
What are Named Queries in JPA?
Named Queries are pre-defined queries stored as annotations, allowing for reusable and optimized queries.
29
What is the Criteria API in JPA?
The Criteria API provides a type-safe way to build dynamic queries programmatically in JPA.