我正在设置一个独立的 JNDI 并将一个数据源加载到 JNDI。我使用的数据源是:org.apache.commons.dbcp.BasicDataSource
JNDI 设置如下
String detectorHost = InetAddress.getLocalHost().getHostName();
System.out.println("detectorHost: " + detectorHost);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
final NamingBeanImpl namingInfo = new NamingBeanImpl();
namingInfo.start();
final Main JNDIServer = new Main();
JNDIServer.setNamingInfo( namingInfo );
JNDIServer.setPort( 5400 );
JNDIServer.setBindAddress(InetAddress.getLocalHost().getHostName());
JNDIServer.start();
final Hashtable _properties = new Hashtable();
_properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
_properties.put(Context.PROVIDER_URL, "jnp://" + InetAddress.getLocalHost().getHostName() + ":5400");
final Context _context = new InitialContext(_properties);
_context.createSubcontext("jdbc");
String JNDI_PATH = "jdbc" + "/" + "mydbname";
_context.bind(JNDI_PATH, getDataSource());
我得到以下异常
javax.naming.CommunicationException [Root exception is java.io.NotSerializableException: org.apache.commons.dbcp.BasicDataSource]
at org.jnp.interfaces.NamingContext.bind(NamingContext.java:677)
at org.jnp.interfaces.NamingContext.bind(NamingContext.java:611)
at javax.naming.InitialContext.bind(Unknown Source)
at com.lombardrisk.reform.integration.ReformIntegration.createJNDIServer(ReformIntegration.java:93)
at com.lombardrisk.reform.integration.ReformIntegration.main(ReformIntegration.java:44)
Caused by: java.io.NotSerializableException: org.apache.commons.dbcp.BasicDataSource
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.rmi.MarshalledObject.<init>(Unknown Source)
at org.jnp.interfaces.MarshalledValuePair.<init>(MarshalledValuePair.java:65)
at org.jnp.interfaces.NamingContext.createMarshalledValuePair(NamingContext.java:1425)
at org.jnp.interfaces.NamingContext.bind(NamingContext.java:640)
我不太明白为什么会收到 NotSerializableException 异常,这是同一 JVM 中的本地 JNDI,而不是远程 JNDI。不知道为什么会这样。
有人可以建议这里有什么问题。
问候 D