从这一行:
package persistence;
import java.util.List;
/**
* Created by Michael
* Creation date 8/20/2017.
* @link https://stackoverflow.com/questions/45787151/com-mysql-jdbc-exception-jdbc4-mysqlnontransientconnectionexception-no-operatio/45787321?noredirect=1#comment78532554_45787321
*/
public interface CategoryDao {
List<String> findAllCategories();
}
我收到错误package database;
import database.util.DatabaseUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Michael
* Creation date 8/20/2017.
* @link https://stackoverflow.com/questions/45787151/com-mysql-jdbc-exception-jdbc4-mysqlnontransientconnectionexception-no-operatio/45787321?noredirect=1#comment78532554_45787321
*/
public class CategoryDaoImpl implements CategoryDao {
private static final Log LOGGER = LogFactory.getLog(CategoryDaoImpl.class);
private static String SELECT_CATEGORIES = "SELECT CategoryName from inventory.Categories ";
private DataSource dataSource;
public CategoryDaoImpl(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public List<String> findAllCategories() {
List<String> categories = new ArrayList<>();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = this.dataSource.getConnection().prepareStatement(SELECT_CATEGORIES);
rs = ps.executeQuery();
while (rs.next()) {
categories.add(rs.getString("CategoryName"));
}
} catch (SQLException e) {
LOGGER.error(String.format("Exception caught while selecting all category names"), e);
} finally {
DatabaseUtils.close(rs);
DatabaseUtils.close(ps);
}
return categories;
}
}
,指向fn b<A>(a: A, b: Copy + Into<A>) {}
位。如果我删除它,它编译。如果我更改定义以采用满足两个特征的附加类型参数,它也会编译。
non-Send/Sync additional trait
答案 0 :(得分:4)
请注意,完整的错误消息是:
error[E0225]: only Send/Sync traits can be used as additional traits in a trait object
--> src/main.rs:1:25
|
1 | fn b<A>(a: A, b: Copy + Into<A>) {}
| ^^^^^^^ non-Send/Sync additional trait
此错误消息未声明Into<A>
是否实施Send
或Sync
;它是说特征Into<A>
本身不是这些特征。
编译器告诉你这是因为编译器假设您故意尝试创建具有多个特征的特征对象。
特质对象的典型示例包括Box<Trait>
或&Trait
。您碰巧在特征本身之前没有任何间接编写代码,这是不受支持的(至少今天不在Rust中),但是编译器将您的代码视为类型声明b: Copy + Into<A>
< / em>一个特质对象,然后找出做出这样假设后出现的问题。
您可以在the book
如果您在特征对象中只编写了一个特征的代码,例如b: std::fmt::Debug
,然后编译器会给你一个不同的错误:
error[E0277]: the trait bound `std::fmt::Debug + 'static: std::marker::Sized` is not satisfied
--> src/main.rs:1:15
|
1 | fn b<A>(a: A, b: std::fmt::Debug) {}
| ^ `std::fmt::Debug + 'static` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `std::fmt::Debug + 'static`
= note: all local variables must have a statically known size
无论如何,在今天的Rust中,你通常不能将多个特征放入一个&#34; trait对象&#34;。但是有一个例外情况:您当前可以使用Send
和/或Sync
作为特征对象中的其他特征。
因此,编译器告诉您,尝试在特征对象中使用Into<A>
作为附加特征是无效的,因为您只能将Send
或Sync
作为附加特征,Into<A>
不是其中之一。