ao tentar incluir uma nova conta injetando Dao e Datasource
type Exception report
message Request processing failed; nested exception is java.lang.RuntimeException: java.sql.SQLException: Connection is closed.
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.RuntimeException: java.sql.SQLException: Connection is closed.
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.RuntimeException: java.sql.SQLException: Connection is closed.
br.com.caelum.contas.dao.ContaDAO.adiciona(ContaDAO.java:49)
br.com.caelum.contas.controller.ContaController.adiciona(ContaController.java:39)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.sql.SQLException: Connection is closed.
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.checkOpen(PoolingDataSource.java:185)
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:312)
br.com.caelum.contas.dao.ContaDAO.adiciona(ContaDAO.java:41)
br.com.caelum.contas.controller.ContaController.adiciona(ContaController.java:39)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.52 logs.
veja código de ContaController, ContaDao e parametrizacao
type Exception report
message Request processing failed; nested exception is java.lang.RuntimeException: java.sql.SQLException: Connection is closed.
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.RuntimeException: java.sql.SQLException: Connection is closed.
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.RuntimeException: java.sql.SQLException: Connection is closed.
br.com.caelum.contas.dao.ContaDAO.adiciona(ContaDAO.java:49)
br.com.caelum.contas.controller.ContaController.adiciona(ContaController.java:39)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.sql.SQLException: Connection is closed.
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.checkOpen(PoolingDataSource.java:185)
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:312)
br.com.caelum.contas.dao.ContaDAO.adiciona(ContaDAO.java:41)
br.com.caelum.contas.controller.ContaController.adiciona(ContaController.java:39)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.52 logs.
package br.com.caelum.contas.controller;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import br.com.caelum.contas.dao.ContaDAO;
import br.com.caelum.contas.modelo.Conta;
@Controller
public class ContaController {
private ContaDAO contaDAO;
@Autowired
public ContaController( ContaDAO contaDAO ) {
this.contaDAO = contaDAO;
}
@RequestMapping("/form")
public String formulario(){
return "formulario";
}
@RequestMapping("/adicionaContaForm")
public String adiciona(@Valid Conta conta, BindingResult result ){
if ( result.hasErrors() ){
return "formulario";
}
this.contaDAO.adiciona(conta);
return "redirect:listaContas";
}
@RequestMapping("/removeConta")
public String remove( Conta conta ){
this.contaDAO.remove(conta);
return "redirect:listaContas";
}
@RequestMapping("/mostraConta")
public String mostra(Long id, Model model) {
model.addAttribute("conta", this.contaDAO.buscaPorId(id));
return "conta/mostra";
}
@RequestMapping("/alteraConta")
public String altera(Conta conta) {
this.contaDAO.altera(conta);
return "redirect:listaContas";
}
@RequestMapping("/listaContas")
public ModelAndView lista(){
List<Conta> contas = contaDAO.lista();
//System.out.println("Numero de registros:"+contas.size());
ModelAndView mv = new ModelAndView("conta/lista");
mv.addObject("todasContas",contas);
return mv;
}
@RequestMapping("/pagaConta")
public void pagaConta( Conta conta, HttpServletResponse resposta ){
contaDAO.paga(conta.getId());
System.out.println("pagamento de Conta via Ajax");
resposta.setStatus(200);
}
}
package br.com.caelum.contas.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import br.com.caelum.contas.modelo.Conta;
import br.com.caelum.contas.modelo.TipoDaConta;
@Repository
public class ContaDAO {
private Connection connection;
@Autowired
public ContaDAO( DataSource ds ){
try {
this.connection = ds.getConnection();
System.out.println("conectou via injecao......................");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void adiciona(Conta conta) {
String sql = "insert into contas (descricao, paga, valor, tipo) values (?,?,?,?)";
PreparedStatement stmt;
try {
stmt = this.connection.prepareStatement(sql);
stmt.setString(1, conta.getDescricao());
stmt.setBoolean(2, conta.isPaga());
stmt.setDouble(3, conta.getValor());
stmt.setString(4, conta.getTipo().name());
stmt.execute();
this.connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void remove(Conta conta) {
if (conta.getId() == null) {
throw new IllegalStateException("Id da conta naoo deve ser nula.");
}
String sql = "delete from contas where id = ?";
PreparedStatement stmt;
try {
stmt = this.connection.prepareStatement(sql);
stmt.setLong(1, conta.getId());
stmt.execute();
this.connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void altera(Conta conta) {
String sql = "update contas set descricao = ?, paga = ?, dataPagamento = ?, tipo = ?, valor = ? where id = ?";
PreparedStatement stmt;
try {
stmt = this.connection.prepareStatement(sql);
stmt.setString(1, conta.getDescricao());
stmt.setBoolean(2, conta.isPaga());
stmt.setDate(3, conta.getDataPagamento() != null ? new Date(conta
.getDataPagamento().getTimeInMillis()) : null);
stmt.setString(4, conta.getTipo().name());
stmt.setDouble(5, conta.getValor());
stmt.setLong(6, conta.getId());
stmt.execute();
this.connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public List<Conta> lista() {
try {
List<Conta> contas = new ArrayList<Conta>();
PreparedStatement stmt = this.connection
.prepareStatement("select * from contas");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
// adiciona a conta na lista
contas.add(populaConta(rs));
}
rs.close();
stmt.close();
this.connection.close();
return contas;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public Conta buscaPorId(Long id) {
if (id == null) {
throw new IllegalStateException("Id da conta nao deve ser nula.");
}
try {
PreparedStatement stmt = this.connection
.prepareStatement("select * from contas where id = ?");
stmt.setLong(1, id);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
this.connection.close();
return populaConta(rs);
}
rs.close();
stmt.close();
this.connection.close();
return null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void paga(Long id) {
if (id == null) {
throw new IllegalStateException("Id da conta nao deve ser nula.");
}
String sql = "update contas set paga = ?, dataPagamento = ? where id = ?";
PreparedStatement stmt;
try {
stmt = this.connection.prepareStatement(sql);
stmt.setBoolean(1, true);
stmt.setDate(2, new Date(Calendar.getInstance().getTimeInMillis()));
stmt.setLong(3, id);
stmt.execute();
this.connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private Conta populaConta(ResultSet rs) throws SQLException {
Conta conta = new Conta();
conta.setId(rs.getLong("id"));
conta.setDescricao(rs.getString("descricao"));
conta.setPaga(rs.getBoolean("paga"));
conta.setValor(rs.getDouble("valor"));
Date data = rs.getDate("dataPagamento");
if (data != null) {
Calendar dataPagamento = Calendar.getInstance();
dataPagamento.setTime(data);
conta.setDataPagamento(dataPagamento);
}
conta.setTipo(Enum.valueOf(TipoDaConta.class, rs.getString("tipo")));
return conta;
}
}
tudo isso começou acontecer na licao 9 que trata IoC. Fiz injeção em UsuarioDAO e aconteceu a mesma coisa... achei q estava fazendo alguma coisa de errado e dei rollback nas alterações de Usuario. Mas mesmo assim ... o erro no browser avisa que a conexão foi fechada.....
obrigado