问题
I want to create oracle connection. Currently i am passing jdbc connection to create struct descriptor and here i am getting exception as below. so to avoid this, required to create a java.sql.connection or oracle connection instead of getting from data source.
org.jboss.resource.adapter.jdbc.jdk6.WrappedConnectionJDK6 cannot be cast to oracle.jdbc.OracleConnection
java.lang.ClassCastException: org.jboss.resource.adapter.jdbc.jdk6.WrappedConnectionJDK6 cannot be cast to oracle.jdbc.OracleConnection
at oracle.sql.StructDescriptor.createDescriptor(StructDescriptor.java:160)
at oracle.sql.StructDescriptor.createDescriptor(StructDescriptor.java:131)
Below is the piece of code where the above exception is coming.
Connection conn = getDataSource().getConnection();
StructDescriptor itemDescriptor = StructDescriptor.createDescriptor(("EMP_DATA", conn);
StructDescriptor addressDescriptor = StructDescriptor.createDescriptor("EMP_ADDRESS", conn);
ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("EMP_TABLE", conn);
/** Creating Objects */
Object[] itemAtributes = new Object[] {
"XXX", "XXX", new STRUCT(addressDescriptor, conn, new Object[] {
"XXX", "XXX", "XXX" }) };
STRUCT itemObject1 = new STRUCT(itemDescriptor, conn, itemAtributes);
itemAtributes = new Object[] {
"YYY", "YYY", new STRUCT(addressDescriptor, conn, new Object[] {
"YYY", "YYY", "YYY" }) };
STRUCT itemObject2 = new STRUCT(itemDescriptor, conn, itemAtributes);
STRUCT[] idsArray = {
itemObject1, itemObject2 };
ARRAY array_to_pass = new ARRAY(descriptor, conn, idsArray);
/** Calling Procedure */
ps = (OraclePreparedStatement) conn.prepareStatement("begin Pr_save_emp(:x); end;");
Help in this regared would be highly appreciated.
回答1:
Error is caused because you are trying to pass JBOSS's WrappedConnection into Oracle's utility class.
According to this https://community.jboss.org/wiki/SetUpAOracleDatasource you need to do the following:
Connection dsConn = getDataSource().getConnection(); //JBoss wrapped connection
Connection conn = ((WrappedConnection)dsConn).getUnderlyingConnection(); //Oracle connection
//everything else remains the same
StructDescriptor itemDescriptor = StructDescriptor.createDescriptor(("EMP_DATA", conn);
//...
来源:https://stackoverflow.com/questions/15086875/how-to-create-oracle-connection-in-spring-application-deployed-in-jboss-server