I have an application that already uses Spring Framework and Spring JDBC with a DAO level using the SimpleJdbcTemplate and RowMapper classes. This seems to work very well with small class structures that are read from the database. However, we need to load objects containing collections of other objects that still store collections of other objects.
The βobviousβ solution to this problem is to create the named RowMapper class or our objects and pass references to the corresponding DAO objects in the constructor. For instance:
public class ProjectRowMapper implements ParameterizedRowMapper { public ProjectRowMapper(AccountDAO accountDAO, ) { this.accountDAO = accountDAO; } public Project mapRow(ResultSet rs, int rowNum) throws SQLException { Project project= new Project (); project.setProjecttId( rs.getString("project_id") ); project.setStartDate( rs.getDate("start_date") );
The problem is that even though ProjectDAO and the DAO account use the same instance of the DataSource (in our case, this is the connection pool), any database calls are made through another connection.
If the hierarchy of objects has three levels of complexity, using this implementation leads to (a) many structure calls to datasource.getConnection () and (2) even worse, since we limit the number of connections allowed in our connection pool to potential race conditions, while while several threads are trying to load a project from the database.
Is there a better way in Spring (without another full-fledged ORM tool) to achieve loading such object hierarchies?
Thanks Paul
source share