0
연결 풀에서 연결을 얻을 수 있습니다 내 DB 설정으로 META-INF에 다음과 같은 XML 파일이 내가 사용하는 연결 풀을 사용하려면정적 클래스는 내가 예를 pool.For 연결의 개념을 이해하려는
<Resource name="jdbc/appname"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
minIdle="10"
username="postgres"
password="123"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/Lab4"/>
내가 MODI 경우 folliwng 클래스는
public class DataBaseConnection {
private static DataSource dataSource;
static {
try {
InitialContext initContext = new InitialContext();
dataSource = (DataSource) initContext.lookup("java:comp/env/jdbc/appname");
} catch (NamingException ex) {
throw new RuntimeException(ex);
}
}
public static Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
이, 질문은 사용
public class UserQueries{
public User selectUserByLoginAndPassword(final String login, final String password) {
try(Connection connection=DataBaseConnection.getConnection())
try (PreparedStatement st = connection.prepareStatement(SELECT_QUERY)) {
st.setString(1, login);
st.setString(2, password);
ResultSet result = st.executeQuery();
while (result.next()) {
final User user = User.newBuilder()
.setId(result.getInt("id"))
.setAge(result.getInt("age"))
.setName(result.getString("name"))
.setPassword(result.getString("password"))
.setLogin(login)
.build();
return user;
}
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
throw new NullPointerException("Nu such user in db");
}
}
입니다 다음과 같은 방법으로 회계 연도 DataBaseConnection는
public class DataBaseConnection {
private DataSource dataSource;
public DataBaseConnection()
try {
InitialContext initContext = new InitialContext();
dataSource = (DataSource) initContext.lookup("java:comp/env/jdbc/appname");
} catch (NamingException ex) {
throw new RuntimeException(ex);
}
public Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
그리고 내가 DB conections를 사용하는 각 클래스의 새로운 DataBaseConnection 객체를 생성합니다, 그것은 (예를 들어 UserQueries에 대한) 각 클래스 분리형 conection에 풀을 사용하기 위해 만드는 것을 의미합니까?
예
public class UserQueries{
private DataBaseConnection dbCon = new DataBaseConnection();
public User selectUserByLoginAndPassword(final String login, final String password) {
try(Connection connection = dbCon.getConnection())
try (PreparedStatement st = connection.prepareStatement(SELECT_QUERY)) {
st.setString(1, login);
st.setString(2, password);
ResultSet result = st.executeQuery();
while (result.next()) {
final User user = User.newBuilder()
.setId(result.getInt("id"))
.setAge(result.getInt("age"))
.setName(result.getString("name"))
.setPassword(result.getString("password"))
.setLogin(login)
.build();
return user;
}
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
throw new NullPointerException("Nu such user in db");
}
}
의 모든 인스턴스 생성됩니다? –
더 나은 결과 : 단일 'DataSource' 인스턴스를 사용하는 것이 좋습니다. "... 내 응용 프로그램은 하나의 연결 풀만 사용하게됩니까?" _-- 예._ – wiseOne