从创建的核心中我可以看到
public class MyContextFactory implements InitialContextFactory {
//Poor Singleton approach. Not thread-safe (But hope you get the idea)
private static InitialContext mockInitialContext;
@Override
public Context getInitialContext(Hashtable<?,?> hshtbl) throws NamingException {
if(mockInitialContext == null) {
mockInitialContext = mock(InitialContext.class);
}
return mockInitialContext;
}
}
public class TestClass {
private DataSource mockDataSource;
private Connection mockConnection;
protected void mockInitialContext() throws NamingException, SQLException {
System.setProperty("java.naming.factory.initial", "com.wrapper.MyContextFactory");
InitialContext mockInitialContext = (InitialContext) NamingManager.getInitialContext(System.getProperties());
mockDataSource = mock(DataSource.class);
mockConnection = mock(Connection.class);
when(mockInitialContext.lookup(anyString())).thenReturn(mockDataSource);
when(mockDataSource.getConnection()).thenReturn(mockConnection);
try {
when(mockDataSource.getConnection()).thenReturn(mockConnection);
} catch (SQLException ex) {
Logger.getLogger(CLASSNAME).log(Level.SEVERE, null, ex);
}
}
发生崩溃的代码是
> A fatal error has been detected by the Java Runtime Environment:
> #
> # SIGSEGV (0xb) at pc=0x00007ffbccaa1b83, pid=1781, tid=0x00007ffbb31ec700
> #
> # JRE version: Java(TM) SE Runtime Environment (8.0_181-b25) (build 1.8.0_181-b25)
> # Java VM: Java HotSpot(TM) 64-Bit Server VM (25.181-b25 mixed mode linux-amd64 compressed oops)
> # Problematic frame:
> # J 6754 C2 java.util.Arrays.equals([B[B)Z (54 bytes) @ 0x00007ffbccaa1b83 [0x00007ffbccaa1b60+0x23]
在崩溃的进程日志中,我发现了很多异常
private List<UnitInfo> unitInfoList = new ArrayList<UnitInfo>(); private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); private final Lock readLock = readWriteLock.readLock(); public UnitInfo getUnitInfoFromIPAddress( InetSocketAddress address )// { byte[] ipAddress = address.getAddress().getAddress(); readLock.lock(); try { UnitInfo unitInfo; Iterator<UnitInfo> list = unitInfoList.iterator(); while( list.hasNext() ) { unitInfo = list.next(); if( null != unitInfo.getIpAddress() ) { if( Arrays.equals( ipAddress, unitInfo.getIpAddress().getAddress() ) ) { return unitInfo; } } } return null; } finally { //Unlock after completing the read operation. readLock.unlock(); } }
这些异常在不久之前出现在许多线程中,并在进程被杀死时停止。
Stack Trace Below: null
java.lang.NullPointerException at
java.util.Arrays.copyOf(Arrays.java:3237)
at common.IpAddress.getAddress(IpAddress.java:161)
很明显,这些NullPointers是由于未初始化byte [] this.address而发生的,并且在执行时碰巧它仍然为null。该错误仅发生一次,所以我在流浪是什么导致了Arrays.equals()内部崩溃的发生?如果参数为null,则此方法不会失败。难道是因为copyOf()中还有另一个NullPointer吗?在那种情况下,我不会在hss_err文件的堆栈跟踪中看到copyOf()吗?迭代器和我的readlock可能是并发问题吗?