weblogic-rdbms-jar.xml中每个实体Bean都要指定data source的JNDI名称(ver 8.1)
<data-source-name>DataSource JNDI Name</data-source-name>
看看你的jar包中指定的名称是否于配置的相符合
<data-source-name>DataSource JNDI Name</data-source-name>
看看你的jar包中指定的名称是否于配置的相符合
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
<enterprise-beans>
<entity>
<display-name>Person</display-name>
<ejb-name>Person</ejb-name>
<home>com.lon.j2ee.ejb.cmp.PersonRemoteHome</home>
<remote>com.lon.j2ee.ejb.cmp.PersonRemote</remote>
<ejb-class>com.lon.j2ee.ejb.cmp.PersonBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>com.lon.j2ee.ejb.cmp.PersonPK</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>Person</abstract-schema-name>
<cmp-field>
<field-name>id</field-name>
</cmp-field>
<cmp-field>
<field-name>name</field-name>
</cmp-field>
<cmp-field>
<field-name>address</field-name>
</cmp-field>
</entity>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Person</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>这是weblogic-ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB//EN' 'http://www.bea.com/servers/wls700/dtd/weblogic-ejb-jar.dtd'>
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>Person</ejb-name>
<entity-descriptor>
<persistence>
<persistence-use>
<type-identifier>WebLogic_CMP_RDBMS</type-identifier>
<type-version>6.0</type-version>
<type-storage>META-INF/weblogic-cmp-rdbms-jar.xml</type-storage>
</persistence-use>
</persistence>
</entity-descriptor>
<jndi-name>PersonRemote</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>这是weblogic-cmp-rdbms-jar.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-rdbms-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB RDBMS Persistence//EN' 'http://www.bea.com/servers/wls700/dtd/weblogic-rdbms20-persistence-700.dtd'>
<weblogic-rdbms-jar>
<weblogic-rdbms-bean>
<ejb-name>Person</ejb-name>
<data-source-name>DataSource</data-source-name>
<table-map>
<table-name>PERSON</table-name>
<field-map>
<cmp-field>id</cmp-field>
<dbms-column>ID</dbms-column>
</field-map>
<field-map>
<cmp-field>name</cmp-field>
<dbms-column>NAME</dbms-column>
</field-map>
<field-map>
<cmp-field>address</cmp-field>
<dbms-column>ADDRESS</dbms-column>
</field-map>
</table-map>
</weblogic-rdbms-bean>
</weblogic-rdbms-jar>
这是bean代码:
package com.lon.j2ee.ejb.cmp;import javax.ejb.*;
import java.sql.Connection;
import javax.naming.InitialContext;
import javax.naming.Context;abstract public class PersonBean
implements EntityBean
{
EntityContext entityContext;
public PersonPK ejbCreate(int id, java.lang.String name,
java.lang.String address) throws CreateException
{
log("EJB has been created");
setId(id);
setName(name);
setAddress(address);
return null;
} public void ejbPostCreate(int id, java.lang.String name,
java.lang.String address) throws CreateException
{
/**@todo Complete this method*/
} public void ejbRemove() throws RemoveException
{
log("EJB has been revoed");
/**@todo Complete this method*/
} public void ejbLoad()
{
log("EJB has been load");
/**@todo Complete this method*/
} public void ejbStore()
{
/**@todo Complete this method*/
} public void ejbActivate()
{
log("EJB has been activited");
/**@todo Complete this method*/
} public void ejbPassivate()
{
/**@todo Complete this method*/
} public void unsetEntityContext()
{
this.entityContext = null;
} public abstract void setId(int id); public abstract void setName(java.lang.String name); public abstract void setAddress(java.lang.String address); public abstract int getId(); public abstract java.lang.String getName(); public abstract java.lang.String getAddress(); public void setEntityContext(EntityContext entityContext)
{
this.entityContext = entityContext;
} /**
* Gets JDBC connection from the connection pool.
*
* @return The JDBC connection
*/
public Connection getConnection() throws Exception
{
try
{
Context ctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.
lookup("java:comp/env/jdbc/oraclePool");
return ds.getConnection();
}
catch (Exception e)
{
System.err.println("Could not locate datasource! Reason:");
e.printStackTrace();
throw e;
}
} private void log(String log)
{
System.out.println("***********: " + log);
}
}
package com.lon.j2ee.info;import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;import com.lon.j2ee.ejb.cmp.PersonPK;
import com.lon.j2ee.ejb.cmp.PersonRemote;
import com.lon.j2ee.ejb.cmp.PersonRemoteHome;/**
* <p>Title: My project</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author long
* @version 1.0
*/public class PersonTestClient1
extends Object
{
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
private static final int MAX_OUTPUT_LINE_LENGTH = 100;
private boolean logging = true;
private PersonRemoteHome personRemoteHome = null;
private PersonRemote personRemote = null; //Construct the EJB test client
public PersonTestClient1()
{
initialize();
} public void initialize()
{
long startTime = 0;
if (logging)
{
log("Initializing bean access.");
startTime = System.currentTimeMillis();
} try
{
//get naming context
Context context = getInitialContext(); //look up jndi name
Object ref = context.lookup("PersonRemote");
//look up jndi name and cast to Home interface
personRemoteHome = (PersonRemoteHome) PortableRemoteObject.narrow(
ref, PersonRemoteHome.class);
if (logging)
{
long endTime = System.currentTimeMillis();
log(
"Succeeded initializing bean access through Home interface.");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch (Exception e)
{
if (logging)
{
log("Failed initializing bean access.");
}
e.printStackTrace();
}
} private Context getInitialContext() throws Exception
{
String url = "t3://gewl:7001";
String user = "username";
String password = "password";
Properties properties = null;
try
{
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null)
{
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS,
password == null ? "" : password);
} return new InitialContext(properties);
}
catch (Exception e)
{
log("Unable to connect to WebLogic server at " + url);
log("Please make sure that the server is running.");
throw e;
}
} public PersonRemote create(int id, String name, String address)
{
long startTime = 0;
if (logging)
{
log("Calling create(" + id + ", " + name + ", " + address + ")");
startTime = System.currentTimeMillis();
}
try
{
personRemote = personRemoteHome.create(id, name, address);
if (logging)
{
long endTime = System.currentTimeMillis();
log("Succeeded: create(" + id + ", " + name + ", " + address +
")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch (Exception e)
{
if (logging)
{
log("Failed: create(" + id + ", " + name + ", " + address + ")");
}
e.printStackTrace();
} if (logging)
{
log("Return value from create(" + id + ", " + name + ", " + address +
"): " + personRemote + ".");
}
return personRemote;
} public PersonRemote findByPrimaryKey(PersonPK pk)
{
long startTime = 0;
if (logging)
{
log("Calling findByPrimaryKey(" + pk + ")");
startTime = System.currentTimeMillis();
}
try
{
personRemote = personRemoteHome.findByPrimaryKey(pk);
if (logging)
{
long endTime = System.currentTimeMillis();
log("Succeeded: findByPrimaryKey(" + pk + ")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch (Exception e)
{
if (logging)
{
log("Failed: findByPrimaryKey(" + pk + ")");
}
e.printStackTrace();
} if (logging)
{
log("Return value from findByPrimaryKey(" + pk + "): " +
personRemote + ".");
}
return personRemote;
}
private void log(String message)
{
} //Main method public static void main(String[] args)
{
PersonTestClient1 client = new PersonTestClient1();
client.create(3,"sadfas","jkjl;kjk;l");
}
}
lookup("java:comp/env/jdbc/oraclePool");
可能是这个的问题
把这个java:comp/env/jdbc/oraclePool直接改为你在weblogic中用的datasorce的jndi名字试试
我按照你的建议,用bmp实现,直接用datasource的jndi的名称DataSource也不行
原来时连接池配置有问题
原来配置为:
Properties
(key=value):一项有如下:username=username
password=password
dll=ocijdbc8
protocol=thin ACLName:username
Password:password改为:
Properties
(key=value):一项有如下:username=username
ACLName:
Password:password注:username,password为数据库访问用户名和密码