java.sql.SQLException: org.apache.tomcat.dbcp.dbcp.DelegatingStatement with address: "com.mysql.jdbc.Statement@a2f8" is closed.
at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.checkOpen(DelegatingStatement.java:137)
at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:206)
这个异常绝对要人郁闷。大概意思是,操作的时候,因为连接已经被关闭了,所以失败出异常。我在网上找烂的答案,结果还是没有。我细细的找代码,没有发现哪里提前关了连接。
终于,1个多小时过去了,找到了错误的原因,是代码的问题。一开始我写成这样:
public class UserService {
private UserDao userDao=new UserDao();
public void saveUserInformationByRegister(String name,String password){
try {
String sql="INSERT INTO user(LoginName,PassWord) VALUES"+"('"+name+"','"+password+"')";
userDao.init();//取得连接
userDao.startTransaction();//开启显示事务
userDao.initStatement();//创建stat语句
userDao.execute(sql);
userDao.commitTransaction();//事务完成
} catch (Exception e) {
userDao.RollBackTransaction();
e.printStackTrace();
}finally{
userDao.release();
}
}
}
引用
问题就是出在private UserDao userDao=new UserDao();上,这里的UserDao永远都是这一个该死的实例,每次掉用这个方法的时候,它都只认这个对象,因为第一次执行成功之后连接已经被关了,所以第2次掉这个方法的时候,怎么掉也没用,连接总是关的。
改成这样:
public class UserService {
public void saveUserInformationByRegister(String name,String password){
UserDao userDao=new UserDao();
try {
String sql="INSERT INTO user(LoginName,PassWord) VALUES"+"('"+name+"','"+password+"')";
userDao.init();//取得连接
userDao.startTransaction();//开启显示事务
userDao.initStatement();//创建stat语句
userDao.execute(sql);
userDao.commitTransaction();//事务完成
} catch (Exception e) {
userDao.RollBackTransaction();
e.printStackTrace();
}finally{
userDao.release();
}
}
}
引用
把UserDao userDao=new UserDao();实例化移到方法体内,每次NEW的对象都不在是同一个,这样一来,自然不会出现以上那种幼稚的错误了。
引用
OK,异常解决了,我继续体验JDBC+SERVLET/JSP带来的没有框架干扰的开发吧。
分享到:
相关推荐
Maven更新问题 今天Maven在更新的时候发现一直更新不成功,总结下解决方法。 在apache-maven-3.5.2/conf/setting.xml中加入... junit Address/ http://jcenter.bintray.com/ central alimaven aliyun maven htt
admin.registry.address=zookeeper://192.168.0.102:2181 admin.config-center=zookeeper://192.168.0.102:2181 admin.metadata-report.address=zookeeper://192.168.0.102:2181 为防止端口冲突我修改为了 ...
本人实践,物超所值,一站式解决jmeter负载机环境问题,问题集合含: ... localhost.localdomain is a loopback address.An error occurred: Cannot start. localhost.localdomain is a loopback address.
9001 dfs.namenode.rpc-address,DataNode会连接这个端口 50070 dfs.namenode.http-address 50470 dfs.namenode.https-address 50100 dfs.namenode.backup.address 50105 dfs.namenode.backup.http-address 50090 ...
nested exception is: java.net.BindException: Address already in use: JVM_Bind 这里说的是1099端口被其它进程占用了. 二.解决办法 找出占用1099端口的进程,进入windows命令,查看什么进程占用了1099端口...
IP Address. . . . . . . . . . . . : 172.16.1.55 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 172.16.1.1 Ethernet adapter 本地连接 2: Media State . . . . . ...
网络管理经典命令网络管理经典命令1.最基本,最常用的测试物理网络的 ping 192.168.0.8 -t ,参数-t是等待用户去中断测试 2.查看DNS、IP、Mac等 A.Win98:winipcfg ...Address: 202.99.160.212
Physical Address. . . . . . . . . : 00-17-31-89-F7-1A Dhcp Enabled. . . . . . . . . . . : No IP Address. . . . . . . . . . . . : 192.168.1.106 Subnet Mask . . . . . . . . . . . : 255.255.255.0 ...
Later is supported when map2dbg.exe tool is placed in \Bin folder of EurekaLog installation (separate download is required) 5)....Added: Exception2HRESULT in EAppDLL to simplify developing DLLs with ...
网络命令行操作 Windows 2k/2003 Server 1.最基本,最常用的,测试物理网络的 ping 192.168.10.88 -t ,参数-t是等待用户去中断测试 2.查看DNS、IP、Mac等 A.Win98:winipcfg ...Address: 202.99.160.212
Address: 202.99.160.68 >server 202.99.41.2 则将DNS改为了41.2 > pop.pcpop.com Server: ns.hesjptt.net.cn Address: 202.99.160.68 Non-authoritative answer: Name: pop.pcpop.com Address: 202.99.160....
IPAddress-master.zip\IPAddress-master\IPAddress\dist\IPAddress.jar https://github.com/seancfoley/IPAddress/ <project name="Create IP Address API" default="run" basedir="."> <description>Create IP ...
compile 'com.smartTop:jd-address:1.0.3' } 使用方法 AddressSelector selector = new AddressSelector(context); selector.setOnAddressSelectedListener(new AddressSelector.OnAddressSelectedListener() { @...
Address: 202.99.160.68 >server 202.99.41.2 则将DNS改为了41.2 > pop.pcpop.com Server: ns.hesjptt.net.cn Address: 202.99.160.68 Non-authoritative answer: Name: pop.pcpop.com Address: 202.99.160....
ESP8266透传固件。适合1Mflash的模块,例如野火,并且支持最新的at_device...ip address: 192.168.101.87 gw address: 192.168.101.1 net mask : 255.255.255.0 dns server #0: 192.168.101.1 dns server #1: 0.0.0.0
Plugin 'FEDERATED' is disabled. 2017-11-15 19:23:46 16c0 InnoDB: Warning: Using innodb_additional_mem_pool_size is DEPRECATED. This option may be removed in future releases, together with the option ...
IP Address. . . . . . . . . . . . : *.*.*.15 Subnet Mask . . . . . . . . . . . : 255.255.255.* Default Gateway . . . . . . . . . : *.*.*.15 Ethernet adapter 无线网络连接: Media State . . ....
先上一张图 FATAL Error: listen EADDRNOTAVAIL 123.57.251.57:5601 配置文件我是这样配置的: [root@head-kib ~]# cd /usr/local/kibana-6.5.4-linux-x86_64/config/ [root@head-kib config]# vim kibana.yml ...