Connection Pool with EJB
ter_d Jan 3, 2005 6:13 AMHello!
I'm trying to connect my database with an EJB and it doesn't work. That's what I do:
1. I configure my jboss.xml in META-INF folder:
<enterprise-beans>
<ejb-name>gestionDB</ejb-name>
<jndi-name>ejb/gestionDB</jndi-name>
<resource-ref>
<res-ref-name>jdbc/OracleDS</res-ref-name>
<jndi-name>java:/OracleDS</jndi-name>
</resource-ref>
</enterprise-beans>
2. I configure my ejb-jar.xml in META-INF folder
<ejb-jar>
<enterprise-beans>
<display-name>gestionDBEJB</display-name>
<ejb-name>gestionDB</ejb-name>
paquetegestionDB.gestionDBHome
paquetegestionDB.gestionDB
<ejb-class>paquetegestionDB.gestionDBEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<resource-ref>
<res-ref-name>jdbc/OracleDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<ejb-name>gestionDB</ejb-name>
<method-name>*</method-name>
<trans-attribute>Supports</trans-attribute>
</container-transaction>
<security-role>
Users
<role-name>users</role-name>
</security-role>
</assembly-descriptor>
</ejb-jar>
3. I zip them and the classes in gestionDB.jar and store it in the deploy folder.
4. That's my Oracle-ds.xml. I know it works cause I use it in an independent servlet.
<local-tx-datasource>
<jndi-name>OracleDS</jndi-name>
<connection-url>jdbc:oracle:thin:@...:...PorDB2</connection-url>
<!--
Here are a couple of the possible OCI configurations.
For more information, see http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/java.920/a96654/toc.htm
<connection-url>jdbc:oracle:oci:@youroracle-tns-name</connection-url>
or
<connection-url>jdbc:oracle:oci:@(description=(address=(host=youroraclehost)(protocol=tcp)(port=1521))(connect_data=(SERVICE_NAME=yourservicename)))</connection-url>
Clearly, its better to have TNS set up properly.
-->
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>...</user-name>
...
<!-- Uses the pingDatabase method to check a connection is still valid before handing it out from the pool -->
<!--valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name-->
<!-- Checks the Oracle error codes and messages for fatal errors -->
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->
<!-- sql to call on an existing pooled connection when it is obtained from pool - the OracleValidConnectionChecker is prefered
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
<type-mapping>Oracle9i</type-mapping>
</local-tx-datasource>
5. gestionDBEJB.java makes the connection with Oracle using this function:
public static Connection conectar()
throws SQLException, RemoteException
{
Context ctx = null;
//Hashtable ht = new Hashtable();
Connection conn = null;
//ht.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
//ht.put(Context.PROVIDER_URL,"localhost");
//ht.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
try
{
//ctx = new InitialContext(ht);
ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:/OracleDS");
conn = ds.getConnection();
System.out.println("Se ha realizado la conexi\363n con \351xito");
return conn;
}
catch(Exception e)
{
System.out.println("Error:" + e);
e.printStackTrace();
return null;
}
}
I do not know why but this does not work. Can anyone help me please?
Thanks in advance
Esther