Hi,
wollt mal fragen wie man Connections mit JSF und Spring am besten Verwaltet.
Mein Dao schaut in etwa so aus:
public class QuestionDao {
protected IAnswerDao _AnswerDao;
protected Connection _Connection;
public QuestionDao(Connection connection, IAnswerDao answerDao) {
_AnswerDao = answerDao;
_Connection = connection;
}
public Question create(Question entity) {
[...]
PreparedStatement prepStatAttachment;
long attachmentid = -1;
try {
prepStatAttachment = _Connection.prepareStatement(insertAttachment);
ResultSet executeQuery = prepStatAttachment.executeQuery();
executeQuery.next();
attachmentid = executeQuery.getLong(1);
entity.setAttachmentId(attachmentid);
prepStatAttachment.close();
} catch (SQLException ex) {
LOGGER.error("create: Shit hit the fan when creating the attachment.", ex);
}
finally
{
if(prepstatattachment !=null) try {prepStatAttachment.close();}
}
[...]
Mein Spring config sieht dann so aus:
<bean id="QuestionDao" class="code.elephant.dao.QuestionDao">
<constructor-arg value="#{dataSource.getConnection()}" index="0" />
<constructor-arg ref="AnswerDao" index="1" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql:// />
<property name="username" value="" />
<property name="password" value=""/>
</bean>
Meine Frage, ist das handling von der getConnection so richtig?