spring4 + jaxb + jpa上的应用程序已部署在Websphere 8.5.5.13和oracle11g上
有时候,我看到错误消息链接具有最大连接池大小:
调用方法时连接不可用 资源的createOrWaitForConnection
那么,正确的解决方法是什么 1.将池连接大小增加到50 2.代码重构
您是否在我的代码中看到一些未公开的连接,使用JPA时是否有粗鲁的错误?
我的服务是:
@Service
public class UserService {
@Autowired
private UserRepository repository;
@Autowired
private EntityManagerFactory entityManagerFactory;
private Logger logger = Logger.getLogger(UserService.class);
public List<User> find(String name, String surname, List<String> types) throws Exception {
logger.log(Level.INFO, "UserService.find()[find by params..]");
Assert.isTrue(name != null || surname != null,
"Один из параметров name или surname обязательны для получения риск-метрики");
List<UserRecord> UserRecords = new ArrayList<>();
if (CollectionUtils.isEmpty(types)) {
UserRecords.addAll(find(name, surname));
} else {
types.stream().forEach(type -> UserRecords.addAll(find(name, surname, type)));
}
return extractUsers(UserRecords);
}
public List<User> find(String id) {
logger.log(Level.INFO, "UserService.find()[find by id]");
UserRecord UserRecord = repository.findOne(id);
if (UserRecord == null) return Collections.emptyList();
User User = (User) deserialize(UserRecord.getUser());
User.setVersion(UserRecord.getVersion());
User.setObjectId(UserRecord.getPrKey());
return Collections.singletonList(User);
}
private List<User> extractUsers(List<UserRecord> UserRecords) {
logger.log(Level.INFO, "UserService.extractUsers()");
if (CollectionUtils.isEmpty(UserRecords) || UserRecords.stream().allMatch(x -> x == null)) {
return new ArrayList<>();
}
return UserRecords.stream().map(UserRecord -> {
User User = (User) deserialize(UserRecord.getUser());
User.setVersion(UserRecord.getVersion());
User.setObjectId(UserRecord.getPrKey());
return User;
}).collect(Collectors.toList());
}
private List<UserRecord> find(String name, String surname, String UserType) {
List<UserRecord> UserRecords;
if (name != null && surname != null) {
UserRecords = repository.findUserRecordsByNameAndSurnameAndUserType(name, surname, UserType);
} else {
if (name == null) {
UserRecords = repository.findUserRecordsBySurnameAndUserType(surname, UserType);
} else {
UserRecords = repository.findUserRecordsByNameAndUserType(name, UserType);
}
}
return UserRecords.stream().allMatch(x -> x == null) ? new ArrayList<>() : UserRecords;
}
private List<UserRecord> find(String name, String surname) {
List<UserRecord> UserRecords;
if (name != null && surname != null) {
UserRecords = repository.findUserRecordsByNameAndSurname(name, surname);
} else {
if (name == null) {
UserRecords = repository.findUserRecordsBySurname(surname);
} else {
UserRecords = repository.findUserRecordsByName(name);
}
}
return UserRecords;
}
}
class UserComparator implements Comparator<User> {
@Override
public int compare(User o1, User o2) {
return Integer.compare(o1.getVersion(), o2.getVersion());
}
}
我的Jpa实体是:
@Entity
@Audited
@Table(name = "userRecord", uniqueConstraints = {
@UniqueConstraint(columnNames = {"name", "surname", "userType", "idCalc"})})
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@AllArgsConstructor
@Getter
@Setter
@Access(AccessType.FIELD)
public class userRecord {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "PR_KEY")
private String prKey;
//Business Key
@Column(name = "name", length = 100, unique = false)
private String name;
//Business Key
@Column(name = "surname", length = 100, nullable = false)
private String surname;
//Business Key
@Column(name = "userType", length = 100, nullable = false)
private String userType;
//Business Key
@Column(name = "idCalc", length = 64, nullable = false)
private String idCalc;
@Version
private int version;
@Column(name = "user", length = 100000)
@Lob
private byte[] user;
public userRecord(String name, String surname, String userType, byte[] user) {
this.name = name;
this.surname = surname;
this.userType = userType;
this.user = user;
}
public userRecord(String name, String surname, String userType,
String idCalc, byte[] user) {
this.name = name;
this.surname = surname;
this.userType = userType;
this.user = user;
this.idCalc = idCalc;
}
}
我的存储库是:
@Repository
public interface RickMetricRepository extends CrudRepository<userRecord, String> {
List<userRecord> finduserRecordsBynameAndsurnameAnduserType(
String name, String surname, String userType);
userRecord finduserRecordBynameAndsurnameAnduserTypeAndIdCalc(
String name, String surname, String userType, String idCalc);
List<userRecord> finduserRecordsBynameAndsurname(String name, String surname);
List<userRecord> finduserRecordsBysurname(String surname);
List<userRecord> finduserRecordsBysurnameAnduserType(String surname, String userType);
List<userRecord> finduserRecordsBynameAnduserType(String name, String userType);
List<userRecord> finduserRecordsByname(String name);
userRecord finduserRecordByPrKey(String id);
}
我需要一些关闭连接操作吗?
答案 0 :(得分:1)
在正常情况下,除非有连接泄漏,否则在很长的时间内都不会使用任何连接。因此,很明显,应用程序没有关闭连接。应用程序完成工作后需要关闭连接。
此问题主要发生在新应用程序部署期间,当应用程序未关闭连接或出现连接泄漏时。每个连接都应采用以下模式:获取/使用/关闭
当您在新应用程序的跟踪中看到此类错误时,首先要检查的是新应用程序中的连接代码模式。它应该遵循get-use-close连接。每个连接都应正确关闭,然后只有连接返回到池中,并准备用于下一个getConnection
调用。如果应用程序没有关闭连接并耗尽连接,则新的getConnection
请求将无法在其ConnectionWaitTimeout
间隔内获得连接。
您可以获得更多信息here。