我正在使用 Spring Boot 和 Dart。当我点击 URL(使用 POSTMEN/浏览器)在 MySql 中插入一些数据时,我得到了正确的响应。但是当我使用 Dart 语言从 Flutter 前端连续发送 3 个请求时,大多数时候返回的是 2 个 GET 请求的结果,并通过第三个请求的错误,并且大多数时候它适用于所有请求。
以下是我在后端用于存储数据的连接服务
。
ConnectionService.java
@Service
public class ConnectionService {
private static final Logger log = LoggerFactory.getLogger(ConnectionService.class);
Connection connection = null;
@Value("${spring.datasource.url}")
String datasourceUrl = "jdbc:mysql://localhost/databaseName?autoReconnect=true&useSSL=false";
public Connection createConnection() throws SQLException {
connection = DriverManager.getConnection(datasourceUrl, "root", "root");
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
return connection;
}
public void closeConnection() {
try {
connection.close();
} catch (Exception e) {
log.error(e.toString());
}
}
}
我正在创建 Connection 服务类的对象,并调用 createConnection()
来创建连接,并调用 closeConnection()
来关闭该连接。
Controller.java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
ConnectionService connectionService;
@GetMapping(path = "/test")
public void testFunction(@RequestParam(name = "abc") String abc) throws SQLException
{
Connection connection = connectionService.createConnection();
if (abc.isExist(param1,param2,connection))
{
//some code
connectionService.closeConnection();
} else
{
//some operation
connectionService.closeConnection();
}
}
指导我解决这个问题。 谢谢
最佳答案
他们的问题是,我不是关闭我在本地创建的实例connection
,而是关闭主connectionService
实例。这就是问题所在。
现在connectionService.closeConnection();
它应该是connection.closeConnection();
关于java - "No operations allowed after connection closed "使用 Dart 和 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60933250/