所以我基本上想在Spring中处理一个带有Controller的HTTP-Post,并在发送数据库之后向User发回一个结果。
所以这是我的例子:
@Controller
public class AngebotController extends WebMvcConfigurerAdapter {
@Autowired
private DatabaseUtils dbUtil;
@Autowired
private MyMailSender mailSender;
@RequestMapping(value = REQUEST_PATH, method = RequestMethod.POST)
public String doPost(@Valid FormInput input, BindingResult bindingResult) {
// .. some input validations here
// after the validation is complete i will have accesss to a object, that i just created, just like the following
// Lets say that this object holds important values for the database query
final MyObject validatedInput = new MyObject();
// Start a new Thread to do the remaining work (the Database Call)
Thread t = new Thread() {
@Override
public void run() {
// so i am starting the database query with the information that i just validated above
// That Database Query will Return a List of Items based on the given MyObject
// This Query will take a long time and i dont want the user to wait, because this data is not nessessary for the user
List<Item> items = dbUtil.getStuffByInput(validatedInput);
for(Item i : items) {
// Now i just want to send some informations about the item via email, this part works
mailSender.sendMail("mail@mail.mail", i);
}
}
}.start();
return "viewname";
}
}
@Service
public class DatabaseUtils {
@Autowired
private ItemRepository repository;
public List<Item> getStuffByInput(MyObject o) {
List<Item> items = repository.findAllByMyObject(o);
// Doing some more stuff with those items here ..
return items;
}
}
// The Implementation will be generated by Spring
public interface ItemRepository extends CrudRepository<Item, Long> {
// will select all Items by comparing the myObject with each Item
// This also works like intended
public List<Item> findAllByMyObject(MyObject myObject);
}
那我的问题在哪里?
我唯一的问题是,数据库查询将结束抛出异常,因为数据库连接已关闭(我猜是春天)
异常:线程“Thread-6”中的异常org.hibernate.SessionException:会话已关闭!
任何帮助表示赞赏。 谢谢!