| |
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| <HTML> |
| <HEAD> |
| <!-- $PAGETITLE --> |
| <TITLE>OpenEJB - Databases</TITLE> |
| <LINK href="http://openejb.apache.org/all.css" rel="stylesheet" type="text/css"> |
| <!--[if IE]><link rel="stylesheet" type="text/css" media="screen, projection" href="openejb.apache.org/ie.css"><![endif]--> |
| |
| <LINK rel="SHORTCUT ICON" href="http://openejb.apache.org/images/favicon.ico"> |
| <SCRIPT language="JavaScript" src="http://cwiki.apache.org/confluence/pages/viewpage.action?spaceKey=OPENEJB&title=functions.js" type="text/javascript"></SCRIPT> |
| <META http-equiv="Content-Type" content="text/html;charset=UTF-8"> |
| </HEAD> |
| <BODY> |
| |
| <!-- Delay the loading of the external javascript file needed for labels (as it takes too long to load and visibly holds loading of the page body) --> |
| <!-- To do this without javascript errors over undefined functions, we need to declare stubs here (that are overrided later by the proper implementations) --> |
| <SCRIPT language="JavaScript" type="text/javascript"> |
| function doAddLabel(hideTextfieldAfterAddParam) |
| { |
| // stub |
| } |
| |
| function onAddLabel() |
| { |
| // stub |
| } |
| |
| function showLabelsInput() |
| { |
| // stub |
| } |
| </SCRIPT> |
| |
| <A name="top"></A> |
| <TABLE class="frameTable" cellpadding="0" cellspacing="0" border="0"> |
| <TR class="Row1"> |
| <TD class="Col1"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col2"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col3"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col4"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col5"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| </TR> |
| <TR class="Row2"> |
| <TD class="Col1"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col2"> </TD> |
| <TD class="Col3" id="breadcrumbs"> |
| <!-- $TOP_NAV_BAR --> |
| |
| |
| |
| |
| |
| <A href="../OPENEJB/index.html" title="Index">Home</A> | <A href="../OPENEJB/download.html" title="Download">Download</A> | <A href="../OPENEJB/mailing-lists.html" title="Mailing Lists">Lists</A> | <SPAN class="nobr"><A href="http://issues.apache.org/jira/browse/OPENEJB" title="Visit page outside Confluence" rel="nofollow">Issues<SUP><IMG class="rendericon" src="http://cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN> |
| </TD> |
| <TD class="Col4"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col5"> </TD> |
| </TR> |
| <TR class="Row3"> |
| <TD class="Col1"><IMG alt="" class="Row3Img" id="thinLine" src="http://openejb.apache.org/images/line_sm.gif"></TD> |
| <TD class="Col2"><IMG alt="" class="Row3Img" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col3"><IMG alt="" class="Row3Img" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col4"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col5"><IMG alt="" class="Row3Img" src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| </TR> |
| <TR class="Row4"> |
| <TD class="Col1"> |
| <SPAN id="Navigation"> |
| |
| |
| |
| |
| |
| |
| <H3><A name="Navigation-Documentation"></A>Documentation</H3> |
| |
| <UL class="alternate" type="square"> |
| <LI><A href="index.html" title="Index">Index</A></LI> |
| <LI><A href="examples.html" title="Examples">Examples</A></LI> |
| </UL> |
| |
| |
| <H3><A name="Navigation-EJB3.0"></A>EJB 3.0</H3> |
| <UL class="alternate" type="square"> |
| <LI><A href="ejb-30-specification.html" title="EJB 3.0 Specification">Specification</A></LI> |
| <LI><A href="callbacks.html" title="Callbacks">Callbacks</A></LI> |
| <LI><A href="security-annotations.html" title="Security Annotations">Security</A></LI> |
| <LI><A href="transaction-annotations.html" title="Transaction Annotations">Transactions</A></LI> |
| <LI><A href="ejb-refs.html" title="EJB Refs">EJB Refs</A></LI> |
| <LI><A href="annotations-xml-and-defaults.html" title="Annotations, XML and Defaults">Defaults</A></LI> |
| </UL> |
| |
| |
| <H3><A name="Navigation-Deployment"></A>Deployment</H3> |
| <UL class="alternate" type="square"> |
| <LI><A href="application-discovery-via-the-classpath.html" title="Application discovery via the classpath">Embedded</A></LI> |
| <LI><A href="deployments.html" title="Deployments">Configured</A></LI> |
| <LI><A href="deploy-tool.html" title="Deploy Tool">Command line</A></LI> |
| </UL> |
| |
| |
| <H3><A name="Navigation-Configuration"></A>Configuration</H3> |
| <UL class="alternate" type="square"> |
| <LI><A href="properties-tool.html" title="Properties Tool">Properties pt1</A></LI> |
| <LI><A href="system-properties.html" title="System Properties">Properties pt2</A></LI> |
| <LI><A href="understanding-the-directory-layout.html" title="Understanding the Directory Layout">Dir Layout</A></LI> |
| <LI><A href="" title="Databases">Databases</A></LI> |
| <LI><A href="clients.html" title="Clients">Clients</A></LI> |
| <LI><A href="jndi-names.html" title="JNDI Names">JNDI Names</A></LI> |
| <LI><A href="security.html" title="Security">Security</A></LI> |
| </UL> |
| |
| |
| <H3><A name="Navigation-Commands"></A>Commands</H3> |
| <UL class="alternate" type="square"> |
| <LI><A href="startup.html" title="Startup">Startup</A></LI> |
| <LI><A href="deploy-tool.html" title="Deploy Tool">Deploy</A></LI> |
| </UL> |
| |
| <H3> |
| <A name="Navigation-Feeds"></A> |
| Feeds |
| </H3> |
| |
| <UL class="feeds"> |
| <LI> |
| <A href="http://cwiki.apache.org/confluence/spaces/rss.action?key=OPENEJB&newPages=false"> |
| <IMG src="http://openejb.apache.org/images/rss.gif"></A> |
| <A class="feedsText" href="http://cwiki.apache.org/confluence/spaces/rss.action?key=OPENEJB&newPages=false">Site</A> |
| </LI> |
| |
| <LI><A href="http://cwiki.apache.org/confluence/spaces/blogrss.action?key=OPENEJB"> |
| <IMG src="http://openejb.apache.org/images/rss.gif"></A> |
| <A class="feedsText" href="http://cwiki.apache.org/confluence/spaces/blogrss.action?key=OPENEJB">News</A> |
| </LI> |
| </UL> |
| </SPAN> |
| </TD> |
| <TD class="Col2"> </TD> |
| <TD class="Col3"> |
| <TABLE id="PageHeader" border="0" width="100%"> |
| <TR> |
| <TD> |
| <A href="http://openejb.org/"> |
| <IMG hspace="0" src="http://openejb.apache.org/images/logo_openejb.gif" vspace="0"> |
| </A> |
| </TD> |
| <TD align="right"> |
| <A href="http://www.apache.org/"> |
| <IMG src="http://www.apache.org/images/asf-logo.gif" width="258" height="66"> |
| </A> |
| </TD> |
| </TR> |
| <TR> |
| <TD id="page_title"> |
| <!-- $TITLE --> |
| Databases |
| </TD> |
| |
| <TD align="right"> |
| <BR><BR> |
| <!-- Google CSE Search Box Begins --> |
| <FORM id="searchbox_010475492895890475512:_t4iqjrgx90" action="http://www.google.com/cse"> |
| <INPUT type="hidden" name="cx" value="010475492895890475512:_t4iqjrgx90"> |
| <INPUT type="hidden" name="cof" value="FORID:0"> |
| <INPUT name="q" type="text" size="25"> |
| <INPUT type="submit" name="sa" value="Search"> |
| </FORM> |
| <SCRIPT type="text/javascript" src="http://www.google.com/coop/cse/brand?form=searchbox_010475492895890475512:_t4iqjrgx90"></SCRIPT> |
| <!-- Google CSE Search Box Ends --> |
| |
| </TD> |
| </TR> |
| </TABLE> |
| <P> |
| <!-- $BODY --> |
| <DIV id="PageContent"> |
| <H2><A name="Databases-ConfiguringDataSourcesinopenejb.xml"></A>Configuring DataSources in openejb.xml</H2> |
| |
| <P>The <B><Resource></B> element is used to configure a <B>javax.sql.DataSource</B>. It is also used to configure other resources like Timers, Topics, Queues. We will see some examples of using <Resource> to configure a DataSource.</P> |
| |
| <P>The <Resource> element is designed after @Resource annotation and has similar attributes.</P> |
| |
| <P>For example, this annotation in your bean:</P> |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-java">@Resource(name = <SPAN class="code-quote">"myDerbyDatasource"</SPAN>, type = javax.sql.DataSource.class)</PRE> |
| </DIV></DIV> |
| |
| <P>Would map to a Resource declared in your openejb.xml as follows:</P> |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-java"><Resource id=<SPAN class="code-quote">"myDerbyDatasource"</SPAN> type=<SPAN class="code-quote">"javax.sql.DataSource"</SPAN>> |
| . . . . |
| <Resource></PRE> |
| </DIV></DIV> |
| |
| <P>Note that in the xml element, the <EM>type</EM> value of <EM>javax.sql.DataSource</EM> can abbreviated to just <EM>DataSource</EM> as follows:</P> |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-java"><Resource id=<SPAN class="code-quote">"myDerbyDatasource"</SPAN> type=<SPAN class="code-quote">"DataSource"</SPAN>> |
| . . . . |
| <Resource></PRE> |
| </DIV></DIV> |
| |
| |
| <H2><A name="Databases-Configurationsforsomecommonlyuseddatabases%3A"></A>Configurations for some commonly used databases:</H2> |
| |
| <H3><A name="Databases-HSQLDB"></A>HSQLDB</H3> |
| |
| <P>The drivers are included with OpenEJB 3.0 and HSQLDB is the default database.</P> |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-xml"><SPAN class="code-tag"><Resource id=<SPAN class="code-quote">"HSQLDB Database"</SPAN> type=<SPAN class="code-quote">"DataSource"</SPAN>></SPAN> |
| JdbcDriver org.hsqldb.jdbcDriver |
| JdbcUrl jdbc:hsqldb:file:hsqldb |
| UserName sa |
| Password |
| <SPAN class="code-tag"></Resource></SPAN></PRE> |
| </DIV></DIV> |
| |
| <H3><A name="Databases-Derby%28Embedded%29"></A>Derby (Embedded)</H3> |
| |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-xml"><SPAN class="code-tag"><Resource id=<SPAN class="code-quote">"Derby Database"</SPAN> type=<SPAN class="code-quote">"DataSource"</SPAN>></SPAN> |
| #Embedded Derby example |
| |
| JdbcDriver org.apache.derby.jdbc.EmbeddedDriver |
| JdbcUrl jdbc:derby:derbyDB;create=true |
| UserName admin |
| Password pass |
| <SPAN class="code-tag"></Resource></SPAN></PRE> |
| </DIV></DIV> |
| |
| <H3><A name="Databases-MySQL"></A>MySQL</H3> |
| |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-xml"><SPAN class="code-tag"><Resource id=<SPAN class="code-quote">"MySQL Database"</SPAN> type=<SPAN class="code-quote">"DataSource"</SPAN>></SPAN> |
| # MySQL example |
| # |
| # This connector will not work until you download the driver at: |
| # http://www.mysql.com/downloads/api-jdbc-stable.html |
| |
| JdbcDriver com.mysql.jdbc.Driver |
| JdbcUrl jdbc:mysql://localhost/test |
| UserName test |
| <SPAN class="code-tag"></Resource></SPAN></PRE> |
| </DIV></DIV> |
| |
| <H3><A name="Databases-Oracle"></A>Oracle</H3> |
| |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-xml"><SPAN class="code-tag"><Resource id=<SPAN class="code-quote">"Oracle Database"</SPAN> type=<SPAN class="code-quote">"DataSource"</SPAN>></SPAN> |
| # Oracle example |
| # |
| # This connector will not work until you download the driver at: |
| # http://otn.oracle.com/software/tech/java/sqlj_jdbc/content.html |
| JdbcDriver com.mysql.jdbc.Driver |
| JdbcUrl jdbc:oracle:thin:@localhost:1521:orcl |
| UserName scott |
| Password tiger |
| <SPAN class="code-tag"></Resource></SPAN></PRE> |
| </DIV></DIV> |
| |
| <H3><A name="Databases-PosgreSQL"></A>PosgreSQL</H3> |
| |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-xml"><SPAN class="code-tag"><Resource id=<SPAN class="code-quote">"PostgreSQL Database"</SPAN> type=<SPAN class="code-quote">"DataSource"</SPAN>></SPAN> |
| # PostgreSQL example |
| # |
| # This connector will not work until you download the driver at: |
| # http://jdbc.postgresql.org/download.html |
| JdbcDriver org.postgresql.Driver |
| JdbcUrl jdbc:postgresql://localhost/test |
| UserName postgres |
| Password pass |
| <SPAN class="code-tag"></Resource></SPAN></PRE> |
| </DIV></DIV> |
| |
| <H3><A name="Databases-InstantDB"></A>InstantDB</H3> |
| |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-xml"><SPAN class="code-tag"><Resource id=<SPAN class="code-quote">"InstantDB Database"</SPAN> type=<SPAN class="code-quote">"DataSource"</SPAN>></SPAN> |
| # InstantDB example |
| # |
| JdbcDriver org.enhydra.instantdb.jdbc.idbDriver |
| JdbcUrl jdbc:idb:conf/instantdb.properties |
| UserName Admin |
| Password pass |
| <SPAN class="code-tag"></Resource></SPAN></PRE> |
| </DIV></DIV> |
| |
| <H2><A name="Databases-JNDInamesforconfiguredDataSources"></A>JNDI names for configured DataSources</H2> |
| |
| |
| <H3><A name="Databases-Example1"></A>Example 1</H3> |
| |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-xml"><SPAN class="code-tag"><Resource id=<SPAN class="code-quote">"Default JDBC Database"</SPAN> type=<SPAN class="code-quote">"DataSource"</SPAN>></SPAN> |
| . . . . . |
| <SPAN class="code-tag"></Resource></SPAN></PRE> |
| </DIV></DIV> |
| <P>The global jndi name would be <B>java:openejb/Resource/Default JDBC Database</B></P> |
| |
| <H3><A name="Databases-Example2"></A>Example 2</H3> |
| |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-xml"><SPAN class="code-tag"><Resource id=<SPAN class="code-quote">"Derby Database"</SPAN> type=<SPAN class="code-quote">"DataSource"</SPAN>></SPAN> |
| . . . . . |
| <SPAN class="code-tag"></Resource></SPAN></PRE> |
| </DIV></DIV> |
| <P>The global jndi name would be <B>java:openejb/Resource/Derby Database</B></P> |
| |
| <H2><A name="Databases-ObtainingaDataSource"></A>Obtaining a DataSource</H2> |
| |
| <P>DataSource references in your ejb should get automatically mapped to the Resource you declare. The shortest and easiest rule is that <B>if your reference name matches a Resource in your openejb.xml, that's the one you get</B>.</P> |
| |
| <P>Alternatively, you can explicitly set them via an openejb-jar.xml. </P> |
| |
| <P>There are various ways one could obtain a DataSource now. Lets take an example of Derby.</P> |
| |
| <P>With a Resource declaration in your openejb.xml like this:</P> |
| |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-xml"><SPAN class="code-tag"><Resource id=<SPAN class="code-quote">"myDerbyDatabase"</SPAN> type=<SPAN class="code-quote">"DataSource"</SPAN>></SPAN> |
| . . . . . |
| <SPAN class="code-tag"></Resource></SPAN></PRE> |
| </DIV></DIV> |
| |
| <P>There are several possible ways to refer to it in your bean code or ejb-jar.xml</P> |
| |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-java">@Stateless |
| <SPAN class="code-keyword">public</SPAN> class FooBean { |
| @Resource DataSource myDerbyDatase; |
| }</PRE> |
| </DIV></DIV> |
| |
| <P><B>OR</B></P> |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-java">@Stateless |
| <SPAN class="code-keyword">public</SPAN> class FooBean { |
| @Resource(name=<SPAN class="code-quote">"myDerbyDatabase"</SPAN>) |
| DataSource dataSource; |
| }</PRE> |
| </DIV></DIV> |
| <P><B>OR</B></P> |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-java">@Resource(name=<SPAN class="code-quote">"myDerbyDatabase"</SPAN>, type=<SPAN class="code-quote">"javax.sql.DataSource"</SPAN>) |
| @Stateless |
| <SPAN class="code-keyword">public</SPAN> class FooBean { |
| |
| <SPAN class="code-keyword">public</SPAN> void setSessionContext(SessionContext sessionContext) { |
| DataSource dataSource = (DataSource) sessionContext.lookup(<SPAN class="code-quote">"myDerbyDatabase"</SPAN>); |
| } |
| |
| <SPAN class="code-keyword">public</SPAN> void someOtherMethod() <SPAN class="code-keyword">throws</SPAN> Exception { |
| InitialContext initialContext = <SPAN class="code-keyword">new</SPAN> InitialContext(); |
| DataSource dataSource = (DataSource) initialContext.lookup(<SPAN class="code-quote">"java:comp/env/myDerbyDatabase"</SPAN>); |
| } |
| }</PRE> |
| </DIV></DIV> |
| <P><B>OR</B></P> |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-xml"><SPAN class="code-tag"><resource-ref></SPAN> |
| <SPAN class="code-tag"><res-ref-name></SPAN>myDerbyDatabase<SPAN class="code-tag"></res-ref-name></SPAN> |
| <SPAN class="code-tag"><res-type></SPAN>javax.sql.DataSource<SPAN class="code-tag"></res-type></SPAN> |
| <SPAN class="code-tag"></resource-ref></SPAN></PRE> |
| </DIV></DIV> |
| <P><B>OR</B></P> |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-xml"><SPAN class="code-tag"><resource-ref></SPAN> |
| <SPAN class="code-tag"><res-ref-name></SPAN>jdbc/myDerbyDatabase<SPAN class="code-tag"></res-ref-name></SPAN> |
| <SPAN class="code-tag"><res-type></SPAN>javax.sql.DataSource<SPAN class="code-tag"></res-type></SPAN> |
| <SPAN class="code-tag"></resource-ref></SPAN></PRE> |
| </DIV></DIV> |
| <P><B>OR</B></P> |
| <DIV class="code"><DIV class="codeContent"> |
| <PRE class="code-xml"><SPAN class="code-tag"><resource-ref></SPAN> |
| <SPAN class="code-tag"><res-ref-name></SPAN>someOtherName<SPAN class="code-tag"></res-ref-name></SPAN> |
| <SPAN class="code-tag"><res-type></SPAN>javax.sql.DataSource<SPAN class="code-tag"></res-type></SPAN> |
| <SPAN class="code-tag"><mapped-name></SPAN>myDerbyDatabase<SPAN class="code-tag"></mapped-name></SPAN> |
| <SPAN class="code-tag"></resource-ref></SPAN></PRE> |
| </DIV></DIV> |
| </DIV> |
| </P> |
| </TD> |
| <TD class="Col4"><IMG src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col5"> |
| |
| |
| </TD> |
| </TR> |
| <TR class="Row5"> |
| <TD class="Col1"> </TD> |
| <TD class="Col2"> </TD> |
| <TD class="Col3"> |
| <BR> |
| <BR> |
| <IMG width="100%" height="1" src="http://openejb.apache.org/images/line_light.gif"> |
| <TABLE width="100%"> |
| <TR> |
| <TD> |
| <SPAN class="bodyGrey"> |
| <SMALL> |
| <NOTICE><!-- $FOOTER --> |
| Apache OpenEJB is an project of The Apache Software Foundation (ASF) |
| </NOTICE> |
| <BR> |
| Site Powered by |
| <A href="http://atlassian.com/">Atlassian</A> |
| <A href="http://atlassian.com/confluence/">Confluence</A> |
| . |
| </SMALL> |
| </SPAN> |
| </TD> |
| <TD align="right"> |
| <A style="color:#999;font-size:small;font-weight:normal;" href="http://cwiki.apache.org/confluence/pages/editpage.action?spaceKey=OPENEJBx30&title=Databases">[ edit ]</A> |
| </TD> |
| </TR> |
| </TABLE> |
| <BR> |
| </TD> |
| <TD class="Col4"><IMG src="http://openejb.apache.org/images/dotTrans.gif"></TD> |
| <TD class="Col5"> </TD> |
| </TR> |
| </TABLE> |
| |
| <!-- Needed for composition plugin --> |
| <!-- delay the loading of large javascript files to the end so that they don't interfere with the loading of page content --> |
| <SPAN style="display: none"> |
| <SCRIPT type="text/javascript" language="JavaScript" src="http://cwiki.apache.org/confluence/labels-javascript"></SCRIPT> |
| |
| </SPAN> |
| |
| <SCRIPT type="text/javascript"> |
| var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); |
| document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); |
| </SCRIPT> |
| <SCRIPT type="text/javascript"> |
| var pageTracker = _gat._getTracker("UA-2717626-1"); |
| pageTracker._initData(); |
| pageTracker._trackPageview(); |
| </SCRIPT> |
| </BODY> |
| </HTML> |