Here is the configuration: MyDataSourceRealm.java:
package test.myrealm; import java.sql.Connection; import javax.naming.InitialContext; import javax.sql.DataSource; public class MyDataSourceRealm extends org.apache.catalina.realm.DataSourceRealm{ @Override protected Connection open() { try{ //it is openejb:Resource not java:openejb:Resource return ((DataSource) new InitialContext().lookup("openejb:Resource/"+dataSourceName)).getConnection(); } catch(Exception e){ e.printStackTrace(); } return null; } } I packaged MyDataSourceRealm in a jar file and drop in lib folder (restart the server) and then add this config context.xml (my app) or server.xml (in tomee config files) <Realm className="test.myrealm.MyDataSourceRealm " dataSourceName="MyDataSource" userTable="userTable" userNameCol="userCol" userCredCol="passCol" userRoleTable="rolTable" roleNameCol="rolCol"> tomee.xml <Resource id="MyDataSource" type="DataSource"> JdbcDriver com.mysql.jdbc.Driver JdbcUrl jdbc:mysql://localhost:3306/mydatabase?autoReconnect=true UserName user Password pass JtaManaged true </Resource> And then it works!!! That's all. Thanks. SCJA. José Luis Cetina 2012/5/15 Romain Manni-Bucau <rmannibu...@gmail.com> > ((DataSource) new > InitialContext().lookup("openejb:Resource/myDataSource")).getConnection() > > sorry i pasted the bad path last time > > - Romain > > > 2012/5/15 Romain Manni-Bucau <rmannibu...@gmail.com> > > > it seems fine, any luck you share this part of the project? > > > > - Romain > > > > > > 2012/5/15 José Luis Cetina <maxtorz...@gmail.com> > > > >> Ok i add my class in a jar file, but im getting: > >> > >> javax.naming.NameNotFoundException:The name > openejb/Resource/MyDataSource > >> does not exist in the current context. > >> > >> is openejb/Resource/MyDataSource the correct directory for use the > >> datasource? > >> > >> 2012/5/15 Romain Manni-Bucau <rmannibu...@gmail.com> > >> > >> > it should be in tomee libs, not your project. (another solution is to > >> use > >> > the snapshot ;) and use the default datasource realm) > >> > > >> > - Romain > >> > > >> > > >> > 2012/5/15 José Luis Cetina <maxtorz...@gmail.com> > >> > > >> > > I did this: > >> > > > >> > > context.mxl > >> > > > >> > > <?xml version="1.0" encoding="UTF-8"?> > >> > > <Context antiJARLocking="true" path="/mavenproject3"> > >> > > <Realm className="realm.test.MyDataSourceRealm" > >> > > dataSourceName="MyDataSource" > >> > > userTable="usertable" > >> > > userNameCol="usercol" > >> > > userCredCol="passcol" > >> > > userRoleTable="roltable" > >> > > roleNameCol="rolcol"/> > >> > > </Context> > >> > > > >> > > > >> > > > >> > > MyDataSourceRealm.java: > >> > > > >> > > package realm.test; > >> > > > >> > > import java.sql.Connection; > >> > > import javax.naming.InitialContext; > >> > > import javax.sql.DataSource; > >> > > > >> > > public class MyDataSourceRealm extends > >> > > org.apache.catalina.realm.DataSourceRealm{ > >> > > > >> > > @Override > >> > > protected Connection open() { > >> > > DataSource dataSource = null; > >> > > try{ > >> > > dataSource = (DataSource)new > >> > > InitialContext().lookup("java:openejb/Resource/" + dataSourceName); > >> > > return dataSource.getConnection(); > >> > > } > >> > > catch(Exception e){ > >> > > e.printStackTrace(); > >> > > } > >> > > return null; > >> > > } > >> > > > >> > > } > >> > > > >> > > > >> > > And i always get ClassNotFoundException: > >> > > > >> > > Severe: Begin event threw exception > >> > > java.lang.ClassNotFoundException: realm.test.KxDataSourceRealm > >> > > > >> > > My class is in my same project, and the package is correct, i dont > >> know > >> > why > >> > > my class cannot be found. > >> > > > >> > > What can be?? > >> > > > >> > > Thanks > >> > > > >> > > > >> > > 2012/5/14 Romain Manni-Bucau <rmannibu...@gmail.com> > >> > > > >> > > > here the issue https://issues.apache.org/jira/browse/TOMEE-191 > >> > > > > >> > > > here a sample > >> > > > > http://openejb.apache.org/examples-trunk/realm-in-tomee/README.html > >> > > > > >> > > > - Romain > >> > > > > >> > > > > >> > > > 2012/5/15 Romain Manni-Bucau <rmannibu...@gmail.com> > >> > > > > >> > > > > seems using org.apache.catalina.realm.DataSourceRealm doesn't > work > >> > out > >> > > of > >> > > > > the box (currently hacking on it). > >> > > > > > >> > > > > extending it and > >> > > > > overriding org.apache.catalina.realm.DataSourceRealm#open() to > >> make: > >> > > > > DataSource dataSource = (DataSource)new > >> > > > > InitialContext().lookup("java:openejb/Resource/" + > >> dataSourceName); > >> > > > should > >> > > > > be enough as a workaround. > >> > > > > > >> > > > > - Romain > >> > > > > > >> > > > > > >> > > > > > >> > > > > 2012/5/14 Jean-Louis MONTEIRO <jeano...@gmail.com> > >> > > > > > >> > > > >> That seems correct to me. > >> > > > >> Can you try adding the property localDataSource=true > >> > > > >> to the following realm definition ? > >> > > > >> > >> > > > >> <Realm > >> > > > >> className="org.apache.catalina.realm.DataSourceRealm" > >> > > > >> dataSourceName="MyDataSource" > >> > > > >> userTable="userTable" > >> > > > >> userNameCol="user" > >> > > > >> userCredCol="pass" > >> > > > >> userRoleTable="rolTable" > >> > > > >> roleNameCol="rol"/> > >> > > > >> > >> > > > >> That should hopefully do the trick as Tomcat will lookup to the > >> > right > >> > > > >> context. > >> > > > >> The only issue that may happen is the order. I mean, may be > >> Tomcat > >> > > gonna > >> > > > >> lookup the resource before TomEE fully initialize and register > >> the > >> > > > >> datasource. > >> > > > >> > >> > > > >> Anyway, let's proceed with the property. We can continue > >> > investigating > >> > > > >> after. > >> > > > >> > >> > > > >> Jean-Louis > >> > > > >> > >> > > > >> 2012/5/14 José Luis Cetina <maxtorz...@gmail.com> > >> > > > >> > >> > > > >> > Hi my web app use openJPA for database CRUD, i configure like > >> > this: > >> > > > >> > > >> > > > >> > tomee.xml: > >> > > > >> > > >> > > > >> > <Resource id="MyDataSource" type="DataSource"> > >> > > > >> > JdbcDriver com.mysql.jdbc.Driver > >> > > > >> > JdbcUrl > >> jdbc:mysql://localhost:3306/mydatabase?autoReconnect=true > >> > > > >> > UserName myuser > >> > > > >> > Password mypass > >> > > > >> > JtaManaged true > >> > > > >> > </Resource> > >> > > > >> > > >> > > > >> > <Resource id="MyUnmanagedDataSource" type="DataSource"> > >> > > > >> > JdbcDriver com.mysql.jdbc.Driver > >> > > > >> > JdbcUrl > >> jdbc:mysql://localhost:3306/mydatabase?autoReconnect=true > >> > > > >> > UserName myuser > >> > > > >> > Password mypass > >> > > > >> > JtaManaged false > >> > > > >> > </Resource> > >> > > > >> > > >> > > > >> > > >> > > > >> > my persistence.xml: > >> > > > >> > > >> > > > >> > <persistence version="2.0" xmlns=" > >> > > > >> http://java.sun.com/xml/ns/persistence" > >> > > > >> > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > >> > > > >> xsi:schemaLocation=" > >> > > > >> > http://java.sun.com/xml/ns/persistence > >> > > > >> > http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> > >> > > > >> > <persistence-unit name="testPU" transaction-type="JTA"> > >> > > > >> > > >> > > > >> > > >> > > > >> > >> > > > > >> > > > >> > > >> > <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> > >> > > > >> > <jta-data-source>MyDataSource</jta-data-source> > >> > > > >> > > >> > > > <non-jta-data-source>MyUnmanagedDataSource</non-jta-data-source> > >> > > > >> > . > >> > > > >> > . > >> > > > >> > . > >> > > > >> > </persistence> > >> > > > >> > > >> > > > >> > > >> > > > >> > MyEJB.java > >> > > > >> > > >> > > > >> > @Stateless > >> > > > >> > public class MyEJB { > >> > > > >> > > >> > > > >> > @PersistenceContext(name="testPU") > >> > > > >> > private EntityManager em; > >> > > > >> > > >> > > > >> > public void test(){ > >> > > > >> > ..... > >> > > > >> > em.createQuery("SELECT o FROM Table > o").getResultList(); > >> > > > >> > ........ > >> > > > >> > } > >> > > > >> > } > >> > > > >> > > >> > > > >> > > >> > > > >> > And all of this works without any trouble. :) > >> > > > >> > > >> > > > >> > > >> > > > >> > Now im trying to use DataSourceRealm with the same Datasource > >> > > > >> (tomee.xml) > >> > > > >> > but doesnt work > >> > > > >> > > >> > > > >> > context.xml > >> > > > >> > > >> > > > >> > <Realm > >> > > > >> > > >> className="org.apache.catalina.realm.DataSourceRealm" > >> > > > >> > dataSourceName="MyDataSource" > >> > > > >> > userTable="userTable" > >> > > > >> > userNameCol="user" > >> > > > >> > userCredCol="pass" > >> > > > >> > userRoleTable="rolTable" > >> > > > >> > roleNameCol="rol"/> > >> > > > >> > > >> > > > >> > I expect the realm take the same DataSource from tomee.xml > but > >> > this > >> > > > >> doesnt > >> > > > >> > happend: > >> > > > >> > > >> > > > >> > Severe: ........ > >> > > > >> > The MyDataSource name is not associated to this context. > >> > > > >> > > >> > > > >> > > >> > > > >> > Is there a way to use the same datasource (tomee.xml) > >> > > > >> > > >> > > > >> > If i use JDBCRealm it works, but i want to reuse the same > >> > datasource > >> > > > >> > config. > >> > > > >> > > >> > > > >> > > >> > > > >> > Any? > >> > > > >> > > >> > > > >> > > >> > > > >> > > >> > > > >> > -- > >> > > > >> > > >> > ------------------------------------------------------------------- > >> > > > >> > SCJA. José Luis Cetina > >> > > > >> > > >> > ------------------------------------------------------------------- > >> > > > >> > > >> > > > >> > > >> > > > >> > -- > >> > > > >> > > >> > ------------------------------------------------------------------- > >> > > > >> > *SCJA. José Luis Cetina* > >> > > > >> > > >> > ------------------------------------------------------------------- > >> > > > >> > > >> > > > >> > >> > > > > > >> > > > > > >> > > > > >> > > > >> > > > >> > > > >> > > -- > >> > > ------------------------------------------------------------------- > >> > > *SCJA. José Luis Cetina* > >> > > ------------------------------------------------------------------- > >> > > > >> > > >> > >> > >> > >> -- > >> ------------------------------------------------------------------- > >> *SCJA. José Luis Cetina* > >> ------------------------------------------------------------------- > >> > > > > > -- ------------------------------------------------------------------- *SCJA. José Luis Cetina* -------------------------------------------------------------------