To group multiple SQL commands and execute them as a single transaction, use the __________ feature in JDBC.
- Batch Processing
- Connection Pooling
- Statement Pooling
- Transaction
The Transaction feature in JDBC allows grouping multiple SQL commands into a single transaction for atomicity.
The method __________ is used to get the vendor-specific error code in an SQL exception.
- getErrorCode()
- getMessage()
- getSQLState()
- printStackTrace()
The getErrorCode() method is used to retrieve the vendor-specific error code from an SQL exception in JDBC.
When refactoring a servlet to make it more configurable, what changes are made regarding initialization parameters?
- Introduce new initialization parameters
- Keep the existing parameters unchanged
- Remove all initialization parameters
- Replace parameters with hardcoding
In the process of making a servlet more configurable, one typically introduces new initialization parameters rather than removing or replacing existing ones. This allows for greater flexibility without disrupting existing functionality.
If an SQL exception occurs in a method that performs multiple database updates, how should the exception be handled to maintain data integrity?
- Commit all changes made before the exception
- Ignore the exception and continue with the remaining updates
- Log the exception and terminate the transaction
- Rollback all changes made before the exception
When an SQL exception occurs during multiple updates, rolling back all changes made before the exception helps maintain data integrity by ensuring either all updates succeed or none do.
When should you choose PreparedStatement over Statement in JDBC?
- Execute multiple queries efficiently
- Execute parameterized queries
- Improve query optimization
- Simplify complex join operations
The PreparedStatement in JDBC is used to execute parameterized queries, which helps prevent SQL injection and improves performance by reusing the compiled query plan. It is suitable when you need to execute the same SQL statement multiple times with different parameters.
What is the advantage of using CallableStatement in JDBC?
- Execute SQL queries with parameters
- Execute stored procedures
- Improve connection pooling
- Streamline complex transactions
The CallableStatement in JDBC is used to execute stored procedures, providing a way to encapsulate and execute multiple SQL statements as a single unit. This enhances code modularity, reusability, and security by allowing the use of stored procedures in the database.
How does transaction isolation level impact database performance and consistency?
- Control concurrency issues
- Ensure data consistency
- Increase transaction throughput
- Read uncommitted data
The transaction isolation level in JDBC determines the degree to which one transaction can affect another. Choosing the appropriate isolation level balances performance and consistency. Lower levels improve performance but may introduce consistency issues, while higher levels enhance consistency at the expense of performance.
How does the use of 'lazy loading' in an ORM framework like Hibernate affect database performance?
- Decreases performance by loading all data at once
- Depends on the specific use case
- Has no impact on performance
- Increases performance by loading data on demand
'Lazy loading' in Hibernate increases performance by loading data on demand, reducing the initial load time and resource consumption.
What are the implications of using optimistic vs. pessimistic locking strategies in database transactions?
- Both have similar implications
- Optimistic locking prevents blocking, may lead to conflicts
- Pessimistic locking reduces contention, but can cause blocking
- They are used interchangeably based on convenience
Optimistic locking in database transactions prevents blocking but may lead to conflicts, while pessimistic locking reduces contention but can cause blocking due to exclusive locks.
Explain the role of database indexing in query optimization.
- Depends on the size of the database and data distribution
- Has no impact on query optimization
- Slows down queries by adding complexity
- Speeds up data retrieval by providing faster access
Database indexing plays a crucial role in query optimization by speeding up data retrieval. Indexes provide faster access to specific data, reducing the time it takes to execute queries, especially in scenarios with large datasets.