在相同的数据上有时会抛出异常java.sql.SQLException:ORA-01002:fetch不按顺序,但在大多数尝试中都工作正常。
在Glassfish 3.1.2.2上运行的Java应用程序。任何人都可以解释一下,问题在哪里?
@Singleton
@LocalBean
@Startup
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class MarketCodesSingleton {
@Resource(mappedName="jdbc/sss")
private DataSource source;
private volatile static Map<Interval, String> marketCodes;
@PostConstruct
@Schedule(minute="*/10", hour="*")
public void fillMarketCodes() {
try(Connection conn = source.getConnection()) {
Map<Interval, String> marketCodesInt = new TreeMap<>();
DaoFactory.getMarketCodesDao().fillMarketCodes(marketCodesInt, conn);
marketCodes = Collections.unmodifiableMap(marketCodesInt);
Logger.getLogger(getClass().getName()).log(Level.FINE, "MarketCodes updated");
} catch (SQLException e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "fillMarketCodes exception",e);
throw new EJBException("fillMarketCodes exception",e);
}
}
public String getMarketCode(Long msisdn) {
Interval interval = new Interval(msisdn);
return marketCodes.get(interval);
}
}
DaoFactory.getMarketCodesDao()fillMarketCodes:
private static final String getMarketCodes_SQL = "CALL SERVICE_PKG.GET_MARKET_CODES(?)";
@Override
public void fillMarketCodes(Map<Interval, String> intervals, Connection conn) throws SQLException {
try (CallableStatement cs = conn.prepareCall(getMarketCodes_SQL)) {
//-10 is a OracleTypes.CURSOR
cs.registerOutParameter(1, -10);
cs.execute();
try (ResultSet rs = (ResultSet) cs.getObject(1)) {
//*******Exception throws on the rs.next() in this method*******
while (rs.next()) {
Interval interval = new Interval(rs.getLong("from_no"), rs.getLong("to_no"));
intervals.put(interval, rs.getString("market_code"));
}
}
}
}
步骤:
procedure GET_MARKET_CODES(
c_cursor OUT SYS_REFCURSOR
) AS
BEGIN
OPEN c_cursor FOR
SELECT from_no, to_no, market_code
FROM market_codes;
END GET_MARKET_CODES;
连接属性:
<jdbc-connection-pool
connection-creation-retry-interval-in-seconds="5"
datasource-classname="oracle.jdbc.xa.client.OracleXADataSource"
max-pool-size="200"
max-connection-usage-count="1000"
res-type="javax.sql.XADataSource"
steady-pool-size="0"
name="sss_pool"
connection-creation-retry-attempts="5">
<property name="URL" value="jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = xxx.xxx.xxx.xxx)(PORT = xx)))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = xx)))"></property>
<property name="Password" value="***"></property>
<property name="User" value="***"></property>
</jdbc-connection-pool>
答案 0 :(得分:1)
代码不完整,所以我只能猜测:
答案 1 :(得分:1)
您的连接是否设置为自动提交?提交或回滚的提取可能会导致此异常。
我还注意到,您的SQL未被Oracle docs或{}中的开始/结束所包围,如此example和Oracle Javadoc。
答案 2 :(得分:1)
确定是否在处理异常时处理所有行
修改以下代码以包含计数器i以获取已处理的行并查找实际的行数
int i=0;
try{
while (rs.next()) {
Interval interval = new Interval(rs.getLong("from_no"), rs.getLong("to_no"));
intervals.put(interval, rs.getString("market_code"));
i=i+1;
}
}
catch (Exception e)
{
Logger.getLogger(getClass().getName()).log(Level.FINE, "the total rows processed"+ i);
Statement stmt = null;
String query = "select count(1) count_rows
from market_codes";
stmt = con.createStatement();
ResultSet rs1 = stmt.executeQuery(query);
rs1.next();
String countRows = rs1.getString("count_rows");
Logger.getLogger(getClass().getName()).log(Level.FINE,"Actual count of rows"+ countRows);
}
答案 3 :(得分:0)
这很奇怪,但是通过删除注释来解决问题@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
任何人都可以解释一下吗?