[JCA/JBoss] - WHICH TYPE OF DATASOURCE SHOULD I USE (CHEAT SHEET)?
by weston.price@jboss.com
JBoss/JCA provides connector managers for three fundamentally different types of JDBC interactions with an underlying RDBMS. The thread breaks breaks down these ConnectionManager options with their associated *-ds.xml configuration type. Note, this is not a replacement for reading relevant documents regarding JCA and Transaction Management in general but rather should be consulted as a quick tip type document.
NoTxConnectionManager -- <no-tx-datasource>
Use this type of configuration if you, the developer, want to completely manage JDBC transactional demarcation within your code.
Example:
|
| InitialContext ctx = null;
| DataSource ds = null;
| Connection conn = null;
|
| try
| {
|
| ctx = new IntialContext();
| ds = (DataSource)ctx.lookup("java:/DefaultDS");
| conn = ds.getConnection();
| conn.setAutoCommit(false); //Transaction begins here
|
| //Do JDBC stuff here
| conn.commit();
|
| }catch (Exception e)
|
| //Evaluate exception, rollback or ignore etc, etc
| conn.rollback();
|
| }finally
| {
|
| //Close other resources Prepared|Callable Statements ehre
| if(con != null)
| con.close();
| }
|
|
TxConnectionManager -- <local-tx-datasource>
Use this type of configuration for JDBC interactions with a *single* datasource. Configurations of this type can be found in JBOSS_HOME/docs/examples/jca. Typically <local-tx-datasource> configurations will be named 'vendor'-ds.xml (ie oracle-ds.xml, postgres-ds.xml, mysql-ds.xml). Note, the important thing to remember is that for this type of configuration you *do not* control transaction boundaries.
Methods from the JDBC API such as setAutoCommit(true | false), commit(), rollback() are not used as JBoss automatically manages the enlistment/delistment of the underlying resource in the current transaction.
TxConnectionManager -- <xa-datasource>
Use this configuration when your application requires the enlistment of multiple resources in the current transaction. Typical scenarios include:
|
| JDBC
| Updating multiple DataSources in the same transaction where the commit or rollback of one DataSource would effect the other and 2PC (two phase commit) is appropriate.
|
| JMS/JDBC
| Receipt of a JMS message and JDBC create/read/update/delete should occur within the same transaction.
|
| JCBC/JCA
| JDBC interaction and XA enabled JCA adapter should occur within the same transaction.
|
|
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3969920#3969920
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3969920
19 years, 8 months
[Tomcat, HTTPD, Servlets & JSP] - Problems with myfaces - ClassCastException JbossActionReques
by commy
Hi there!
i've got a problem with my myfaces portlet.
With the following set of files, i can add resources, but when i click on Edit, i get the following Exception:
Here's the bean, called Resource.java
| /*
| * Resource.java
| *
| * Created on September 4, 2006, 9:58 PM
| *
| * To change this template, choose Tools | Template Manager
| * and open the template in the editor.
| */
|
| package iro.model;
|
| import java.io.Serializable;
| import javax.persistence.Entity;
| import javax.persistence.GeneratedValue;
| import javax.persistence.GenerationType;
| import javax.persistence.Id;
| import javax.persistence.Basic;
|
| /**
| *
| * @author root
| */
| @Entity
| public class Resource implements Serializable {
|
| @Id
| @GeneratedValue(strategy = GenerationType.AUTO)
| private Long id;
| @Basic
| private String name;
| @Basic
| private String description;
|
| /** Creates a new instance of Resource */
| public Resource() {
| }
|
| public String getName(){
| return this.name;
| }
|
| public void setName(String newName){
| this.name = newName;
| }
|
| public String getDescription(){
| return this.description;
| }
|
| public void setDescription(String newDescription){
| this.description = newDescription;
| }
|
| public Long getId() {
| return id;
| }
|
| public void setId(Long id) {
| this.id = id;
| }
|
| public String toString() {
| //TODO change toString() implementation to return a better display name
| return "" + this.id;
| }
|
| }
|
|
here are the jsf files...
Detail.jsf
| <%@page contentType="text/html"%>
| <%@page pageEncoding="UTF-8"%>
| <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
| <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
| <head>
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
| <title>Detail of Resource</title>
| </head>
| <f:view>
| <h:messages style="color: green" layout="table"/>
| <h1>Detail of resource</h1>
| <h:form>
| <h:panelGrid columns="2">
| <h:outputText>Name:</h:outputText>
| <h:outputText value="#{resource.resource.name}" title="Name" />
| <h:outputText>Description:</h:outputText>
| <h:outputText value="#{resource.resource.description}" title="Description" />
| <h:outputText>Id:</h:outputText>
| <h:outputText value="#{resource.resource.id}" title="Id" />
| </h:panelGrid>
| <h:commandLink action="#{resource.editSetup}">
| <f:param name="id" value="#{resource.resource.id}"/>
| <h:outputText value="Edit"/>
| </h:commandLink>
| <br>
| <h:commandLink action="resource_list" value="Show All Resource"/>
| <br>
| <a href="/WebApplication11/index.jsp">Back to index</a>
| </h:form>
| </f:view>
|
Edit.jsf
| <%@page contentType="text/html"%>
| <%@page pageEncoding="UTF-8"%>
| <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
| <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
| <head>
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
| <title>Edit Resource</title>
| </head>
| <f:view>
| <h:messages style="color: green" layout="table"/>
| <h1>Edit resource</h1>
| <h:form>
| <h:panelGrid columns="2">
| <f:verbatim>Name</f:verbatim>
| <h:inputText id="name" value="#{resource.resource.name}" title="Name" />
| <f:verbatim>Description</f:verbatim>
| <h:inputText id="description" value="#{resource.resource.description}" title="Description" />
| </h:panelGrid>
| <h:commandLink action="#{resource.edit}" value="Save"/>
| <br>
| <h:commandLink action="resource_list" value="Show All Resource"/>
| <br>
| <a href="/WebApplication11/index.jsp">Back to index</a>
| </h:form>
| </f:view>
|
|
List.jsf
| <%@page contentType="text/html"%>
| <%@page pageEncoding="UTF-8"%>
| <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
| <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
| <head>
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
| <title>List Resource</title>
| </head>
| <f:view>
| <h:messages style="color: green" layout="table"/>
| <h1>Listing Resources</h1>
| <h:form>
| <h:dataTable value='#{resource.resources}' var='item' border="1" cellpadding="2" cellspacing="0">
| <h:column>
| <f:facet name="header">
| <h:outputText value="Name"/>
| </f:facet>
| <h:outputText value="#{item.name}"/>
| </h:column>
| <h:column>
| <f:facet name="header">
| <h:outputText value="Description"/>
| </f:facet>
| <h:outputText value="#{item.description}"/>
| </h:column>
| <h:column>
| <f:facet name="header">
| <h:outputText value="Id"/>
| </f:facet>
| <h:commandLink action="#{resource.detailSetup}">
| <f:param name="id" value="#{item.id}"/>
| <h:outputText value="#{item.id}"/>
| </h:commandLink>
| </h:column>
| <h:column>
| <h:commandLink action="#{resource.destroy}">
| <f:param name="id" value="#{item.id}"/>
| <h:outputText value='Destroy'/>
| </h:commandLink>
| <h:outputText value=" "/>
| <h:commandLink action="#{resource.editSetup}" value="Edit">
| <f:param name="id" value="#{item.id}"/>
| </h:commandLink>
| </h:column>
| </h:dataTable>
| <h:commandLink action="#{resource.createSetup}">
| <h:outputText value="New Resource"/>
| </h:commandLink>
| <br>
| <a href="/WebApplication11/index.jsp">Back to index</a>
| </h:form>
| </f:view>
|
New.jsf
| <%@page contentType="text/html"%>
| <%@page pageEncoding="UTF-8"%>
| <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
| <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
| <head>
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
| <title>New Resource</title>
| </head>
| <f:view>
| <h:messages style="color: green" layout="table"/>
| <h1>New resource</h1>
| <h:form>
| <h:panelGrid columns="2">
| <f:verbatim>Name</f:verbatim>
| <h:inputText id="name" value="#{resource.resource.name}" title="Name" />
| <f:verbatim>Description</f:verbatim>
| <h:inputText id="description" value="#{resource.resource.description}" title="Description" />
| </h:panelGrid>
| <h:commandLink action="#{resource.create}" value="Create"/>
| <br>
| <h:commandLink action="resource_list" value="Show All Resource"/>
| <br>
| <a href="/WebApplication11/index.jsp">Back to index</a>
| </h:form>
| </f:view>
|
|
Here's the faces-config.xml
| <?xml version="1.0" encoding="UTF-8"?>
| <!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
| <!-- =========== FULL CONFIGURATION FILE ================================== -->
| <faces-config xmlns="http://java.sun.com/JSF/Configuration">
| <converter>
| <converter-for-class>iro.model.Resource</converter-for-class>
| <converter-class>iro/controller.ResourceConverter</converter-class>
| </converter>
| <managed-bean>
| <managed-bean-name>resource</managed-bean-name>
| <managed-bean-class>iro.controller.ResourceController</managed-bean-class>
| <managed-bean-scope>session</managed-bean-scope>
| </managed-bean>
| <navigation-rule>
| <from-view-id>/iro.view/resource/*</from-view-id>
| <navigation-case>
| <from-outcome>resource_create</from-outcome>
| <to-view-id>/iro.view/resource/New.jsp</to-view-id>
| </navigation-case>
| <navigation-case>
| <from-outcome>resource_list</from-outcome>
| <to-view-id>/iro.view/resource/List.jsp</to-view-id>
| </navigation-case>
| <navigation-case>
| <from-outcome>resource_edit</from-outcome>
| <to-view-id>/iro.view/resource/Edit.jsp</to-view-id>
| </navigation-case>
| <navigation-case>
| <from-outcome>resource_detail</from-outcome>
| <to-view-id>/iro.view/resource/Detail.jsp</to-view-id>
| </navigation-case>
| </navigation-rule>
| <lifecycle/>
| <application>
| <locale-config/>
| </application>
| <factory/>
| </faces-config>
|
|
the portlet.xml
| <?xml version="1.0" encoding="UTF-8"?>
| <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
| xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd /opt/SUNWps/dtd/portlet.xsd"
| version="1.0">
| <!-- RESOURCE PORTLET -->
| <portlet>
| <portlet-name>IroResourcePortlet</portlet-name>
| <portlet-class>org.apache.myfaces.portlet.MyFacesGenericPortlet</portlet-class>
| <init-param>
| <name>default-view</name>
| <value>/iro.view/resource/List.jsp</value>
| </init-param>
| <supports>
| <mime-type>text/html</mime-type>
| <portlet-mode>VIEW</portlet-mode>
| </supports>
| <portlet-info>
| <title>IroResourcePortlet</title>
| </portlet-info>
| </portlet>
| </portlet-app>
|
and here's the web.xml
| <?xml version="1.0" encoding="UTF-8"?>
| <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
| <listener>
| <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
| </listener>
| </web-app>
|
i already tried to add a jsf mapping to the web.xml, but it didn't work.
Caused by: java.lang.ClassCastException: org.jboss.portlet.JbossActionRequest cannot be cast to javax.servlet.http.HttpServletRequest
can anyone help me?
greetings markus
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3969919#3969919
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3969919
19 years, 8 months
[JCA/JBoss] - Re: XA Connection error
by weston.price@jboss.com
Note, one more word to the wise :-)
If you don't need XA (enlistment of multiple resources in a transaction) then don't use it and use a <local-tx-datasource>.
XA is typically used for things like updating multiple DB's in the same UOW, JMS and JDBC in the same transaction etc. etc.
There are examples of <local-tx-datasource> configurations in the JBOSS_ROOT/doc/examples/jca directory. For your stuff, you will want to look at oracle-ds.xml. The new *-ds.xml file would look something like
| <datasources>
| <local-tx-datasource>
| <jndi-name>OracleDS</jndi-name>
| <track-connection-by-tx/>
| <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
| <user-name>x</user-name>
| <password>y</password>
| <connection-url>yoururl</connection-url>
| <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
| <no-tx-separate-pools/>
| <!--pooling parameters-->
| <min-pool-size>1</min-pool-size>
| <max-pool-size>100</max-pool-size>
| <blocking-timeout-millis>5000</blocking-timeout-millis>
| <idle-timeout-minutes>15</idle-timeout-minutes>
| <metadata>
| <type-mapping>Oracle9i</type-mapping>
| </metadata>
| </local-tx-datasource>
| </datasources>
|
Obviously you will want to replace the properties in config with your own.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3969917#3969917
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3969917
19 years, 8 months
[JCA/JBoss] - Re: XA Connection error
by weston.price@jboss.com
Not a problem.
So option 2 it is :-)
anonymous wrote :
| If I omit the UserTransaction, then the environment (JBoss) will create one anyway for each Connection I open. Are these correct assumptions?
|
Nope.
In option 2, you are responsible for creating the UserTransaction and 'demarcating' the transaction boundaries (note, this is much the same as you would do with JDBC, just at a different layer (Servlet/JSP).
Option 1, EJB, gives you declarative (automatic) transaction support.
Everything is bound and ready to go in JNDI when JBoss starts up so there is nothing special you need to do in this regard. Just get the UserTransaction, start the transaction and commit/rollback the transaction when necessary.
Again, this is very much like JDBC just at a different layer.
Note, this is a primary reason people often use EJB/Spring or some other framework that automatically does the start/commit/rollback. Servlets/JSF have no such declarative transaction model though you can use other frameworks to achieve these results inside a Web environment.
Again, I am not a JSF expert so there may be some tag/configuration that will start/rollback/commit for you. You may want to take a look at the JSF doc for clarification.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3969914#3969914
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3969914
19 years, 8 months
[JCA/JBoss] - Re: XA Connection error
by smithbstl
Again, I appreciate you taking the time to explain all of this. I am not using EJB, so I will go with "option 2". Basically you are saying that I should wrap my JDBC inside of a "UserTransaction". This abstracts the actual database transaction from me. If I omit the UserTransaction, then the environment (JBoss) will create one anyway for each Connection I open. Are these correct assumptions?
To use the UserTransaction (interface), I need to perform a JNDI lookup like you have shown? Based on the following, the UserTransaction MBean requires no special configuration on my part and is automatically published by JBoss?
http://docs.jboss.org/jbossas/admindevel326/html/ch4.chapt.html
anonymous wrote : To use the UserTransaction interface in other places, the org.jboss.tm.usertx.server.ClientUserTransactionService MBean must be configured and started. This MBean publishes a UserTransaction implementation under the JNDI name UserTransaction. This MBean is configured by default in the standard JBoss distributions and has no configurable attributes.
Thanks again for all your help.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3969913#3969913
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3969913
19 years, 8 months