Spring Interview Questions

How can you create a DataSource connection pool?

To create a DataSource connection pool :


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driver">
<value>${db.driver}</value>
</property>
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
</bean>

List the Springs’s DAO exception hierarchy?

Springs’s DAO exception hierarchy is :
* CleanupFailureAccessException
* DataAccessResourceFailureException
* DataIntegrityViolationException
* DataRetrievalFailureException
* DeadlockLoserDataAccessException
* IncorrectUpdateSemanticsDataAccessException
* InvalidDataAccessApiUsageException
* InvalidDataAccessResourceUsageException
* OptimisticLockingFailureException
* TypeMismatchDataAccessException
* UncategorizedDataAccessException

How do you configure your database driver in spring?

To configure your database driver using datasource “org.springframework.jdbc.datasource.DriverManagerDataSource”. For Example:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>jdbc:hsqldb:db/appfuse</value>
</property>
<property name="username"><value>sa</value></property>
<property name="password"><value></value></property>
</bean>

How can you configure JNDI instead of datasource in spring applicationcontext.xml?

You can configure JNDI instead of datasource in spring applicationcontext.xml using “org.springframework.jndi.JndiObjectFactoryBean”. For Example:


<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/appfuse</value>
</property>
</bean>

Explain about PreparedStatementCreator?

The PreparedStatementCreator interface is a callback interfaces used by the JdbcTemplate class. This interface creates a PreparedStatement given a connection, provided by the JdbcTemplate class. Implementations are responsible for providing SQL and any necessary parameters. A PreparedStatementCreator should also implement the SqlProvider interface if it is able to provide the SQL it uses for PreparedStatement creation. This allows for better contextual information in case of exceptions.
It has one method:
PreparedStatement createPreparedStatement(Connection con) throws SQLException
Create a statement in this connection. Allows implementations to use PreparedStatements. The JdbcTemplate will close the created statement.

Explain about BatchPreparedStatementSetter?

BatchPreparedStatementSetter interface sets values on a PreparedStatement provided by the JdbcTemplate class for each of a number of updates in a batch using the same SQL. Implementations are responsible for setting any necessary parameters. SQL with placeholders will already have been supplied. Implementations of BatchPreparedStatementSetter do not need to concern themselves with SQLExceptions that may be thrown from operations they attempt. The JdbcTemplate class will catch and handle SQLExceptions appropriately.
BatchPreparedStatementSetter has two method:
* int getBatchSize() :-  Return the size of the batch.
* void setValues(PreparedStatement ps, int i) :-Set values on the given PreparedStatement.

Explain about RowCallbackHandler and why it is used?

RowCallbackHandler interface is used by JdbcTemplate for processing rows of a ResultSet on a per-row basis. Implementations of this interface perform the actual work of processing each row but don’t need to worry about exception handling. SQLExceptions will be caught and handled by the calling JdbcTemplate. RowCallbackHandler object is typically stateful: It keeps the result state within the object, to be available for later inspection.
RowCallbackHandler interface has one method :
void processRow(ResultSet rs)  -Implementations must implement this method to process each row of data in the ResultSet.

What classes are used to Control the database connection in Spring’s JDBC API?

The classes that are used to Control the database connection in Spring’s JDBC API are :
* DataSourceUtils
* SmartDataSource
* AbstractDataSource
* SingleConnectionDataSource
* DriverManagerDataSource
* TransactionAwareDataSourceProxy
* DataSourceTransactionManager

Explain DAO in Spring framework?

Spring Framework’s Data Access Object (DAO) support provides integration of heterogeneous Java Database Connectivity (JDBC) and Object-Relational Mapping (ORM) products. DAO support is provided for JDBC, Hibernate, iBATIS, Java Data Objects (JDO), Java Persistence API (JPA), Oracle’s TopLink and Common Client Interface (CCI).

Define a Pointcut in Spring?

Spring defines pointcuts in terms of the class and method that is being advised. Advice is woven into the target class and its methods are based on their characteristics, such as class name and method signature. The core interface for Spring’s pointcut framework is, naturally, the Pointcut interface.