我试图在rails中实现多线程方法,以便我可以非常快速地创建/更新多个记录.
这是我的计划的大纲.
ActiveRecord::Base.transaction do
(1..10).each do |i|
arr[i] = Thread.new {
<some logic on obj>
...
...
obj.save!
}
end
arr.each {|t| t.join}
end
Run Code Online (Sandbox Code Playgroud)
这给了我日志上的警告.
DEPRECATION WARNING: Database connections will not be closed automatically,
please close your database connection at the end of the thread by calling `close`
on your connection.
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误
could not obtain a database connection within 5 seconds (waited 5.059358 seconds).
The max pool size is currently 5; consider increasing it.
Run Code Online (Sandbox Code Playgroud)
我试过: - 更改database.yaml并增加poolize和timeout. - 以下列方式修改现有代码.
ActiveRecord::Base.connection_pool.clear_stale_cached_connections! …Run Code Online (Sandbox Code Playgroud) 我目前正在允许用户选择某些参数,并根据这些参数,我生成一个csv文件并将其作为下载推送给用户.例如
send_data <generated csv data>, :disposition => 'attachment' :type => 'text/csv'
Run Code Online (Sandbox Code Playgroud)
有时,由于数据变得太大而无法计算,我不希望让用户等待将文件作为下载推送.我想将此文件作为附件发送到电子邮件中.
我可以正常发送电子邮件.我可以将已存在的文件作为附件发送.我不想保存这个文件.我想直接通过电子邮件发送给用户.
我怎么做?
我的代码是这样设置的。
abstract class BaseController {
@Inject Store store;
}
class MyController extends BaseController {
private final Validator validator;
@Inject
public MyController(Validator validator) {
this.validator = validator;
}
public boolean someMethod() {
a = store.storingMethod();
b = validator.validate(a);
...
...
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想为myController. 在测试中,我想使用注入的Store但我想模拟Validator. 我试过这样的事情。
@RunWith(MockitoJUnitRunner.class)
public class MyControllerTest() {
private MyController myController;
@Mock private Validator validator;
@Before
public void before() {
myController = new MyController(validator);
}
}
Run Code Online (Sandbox Code Playgroud)
我知道,如果我Store store从BaseController …