Skip navigation

EJB/AS Rookie

2 posts

You need to transform a String into a CLOB. JBoss 6 uses Hibernate 3.6 and there are some changes to the CLOB/BLOB creation. You need to access Hibernate session to get a reference to a LobHelper implementation.

 

There're two ways how you can access the session:

  1. You can inject your Persistence context into a org.hibernate.Session variable, see Bill Burke's blog here
  2. You access the infamous getDelegate() method and cast to org.hibernate.Session, i.e. something like:

 

{code:java}

@PersistenceContext(

            unitName = "MY_UNIT")

    private EntityManager eMan;

//in some method

String strText = "A very long text";

org.hibernate.Session se = (org.hibernate.Session) eMan.getDelegate();

LobHelper lh = se.getLobHelper();

Clob cl = lh.createClob(strText);

{code}

Why "infamous"? The books I read about EJB3 told me to be careful when using specific JPA implementation features (in this case, Hibernate), i.e. the application will be tied to this particular implementation of the JPA.

 

See the Hibernate API on LobHelper for further methods.

 

UPDATE:

Unfortunately, I hit this documented bug: the cast into Oracle Clob doesn't work. The link contains also the workaround. Basically, you have to use a String field instead of Clob. Something like this:

 

{code:java}

@Lob

    @Basic(

            fetch = FetchType.LAZY)

    @Type(

            type = "org.hibernate.type.StringClobType")

    private String someStuff;

{code}

So you don't need the LobHelper at all in this case.

You want your application logs to be inside a specific log file only, instead of server.log.

 

Here's the brief instruction:

 

  1. Create a jboss-logging.xml file (you may copy it from the /server/default/delpoy directory) and put it into your project's META-INF or WEB-INF folder
  2. Define a context and a handler
  3. Refer to the handler from the root logger portion of the xml file
  4. Modify the file logmanager-jboss-beans.xml of your server configuration

 

Here's the jboss-logging.xml I use:

 

{code:xml}

<?xml version="1.0" encoding="UTF-8"?>

 

<!-- ===================================================================== -->

<!-- -->

<!-- Logging System Configuration -->

<!-- -->

<!-- ===================================================================== -->

 

<logging xmlns="urn:jboss:logging:6.0" context="MyAppLoggingContext">

    <define-context name="MyAppLoggingContext" />

 

    <!-- ================================= -->

    <!-- Preserve messages in a local file -->

    <!-- ================================= -->

 

    <!-- A time/date based rolling handler -->

 

    <size-rotating-file-handler file-name="${jboss.server.log.dir}/Mercurius.log"

        name="MERCURIUS" autoflush="true" append="true" rotate-size="1024k"

        max-backup-index="5">

 

        <error-manager>

            <only-once />

        </error-manager>

 

        <formatter>

            <!-- To revert back to simple stack traces without JAR versions, change

                "%E" to "%e" below. -->

            <!-- Uncomment this to get the class name in the log as well as the category

                <pattern-formatter pattern="%d %-5p [%c] %C{1} (%t) %s%E%n"/> -->

            <!-- Uncomment this to log without the class name in the log -->

            <pattern-formatter pattern="%d %-5p [%c] (%t) %s%E%n" />

        </formatter>

    </size-rotating-file-handler>

 

 

    <!-- Limit the org.apache category to INFO as its DEBUG is verbose -->

    <logger category="org.apache">

        <level name="INFO" />

    </logger>

 

    <!-- Limit the jacorb category to WARN as its INFO is verbose -->

    <logger category="jacorb">

        <level name="WARN" />

    </logger>

 

    <!-- Limit JSF to INFO as its FINE is verbose -->

    <logger category="javax.enterprise.resource.webcontainer.jsf">

        <level name="INFO" />

    </logger>

 

    <!-- Limit the org.jgroups category to WARN as its INFO is verbose -->

    <logger category="org.jgroups">

        <level name="WARN" />

    </logger>

 

    <!-- Limit the org.quartz category to INFO as its DEBUG is verbose -->

    <logger category="org.quartz">

        <level name="INFO" />

    </logger>

 

    <!-- Limit the com.sun category to INFO as its FINE is verbose -->

    <logger category="com.sun">

        <level name="INFO" />

    </logger>

 

    <!-- Limit the sun category to INFO as its FINE is verbose -->

    <logger category="sun">

        <level name="INFO" />

    </logger>

 

    <!-- Limit the javax.xml.bind category to INFO as its FINE is verbose -->

    <logger category="javax.xml.bind">

        <level name="INFO" />

    </logger>

 

    <!-- Limit the springframework category to WARN -->

    <logger category="org.springframework">

        <level name="WARN" />

    </logger>

 

    <!-- Test -->

    <logger category="it.bz">

        <level name="DEBUG" />

    </logger>

 

    <!-- Limit Arjuna transaction manager -->

    <logger category="com.arjuna.ats">

        <level name="INFO" />

    </logger>

 

    <!-- Limit Hibernate <logger category="org.hibernate"> <level name="INFO"

        /> </logger> -->

 

    <!-- Limit Ajax4jsf <logger category="org.ajax4jsf"> <level name="INFO"

        /> </logger> -->

 

    <!-- Limit JNP <logger category="org.jnp"> <level name="INFO" /> </logger> -->

 

 

    <!-- Limit the JSR77 categories -->

    <logger category="org.jboss.management">

        <level name="INFO" />

    </logger>

 

    <!-- Limit the verbose facelets compiler -->

    <!-- Also suppress error with legacy facelets (JBAS-7600) -->

    <logger category="facelets.compiler">

        <level name="WARN" />

        <filter>

            <not>

                <match pattern="Error\sLoading\sLibrary.*jsf-libs/jsf-impl" />

            </not>

        </filter>

    </logger>

 

    <logger category="org.jboss.serial">

        <level name="INFO" />

    </logger>

 

 

 

    <!-- Reduce org.mc4j stuff in the logs -->

    <logger category="org.mc4j">

        <level name="WARN" />

    </logger>

 

 

    <logger category="org.rhq.plugins.jbossas5.ApplicationServerComponent">

        <level name="FATAL" />

    </logger>

 

    <!-- ======================= -->

    <!-- Setup the Root category -->

    <!-- ======================= -->

 

    <root-logger>

        <!-- Set the root logger priority via a system property, with a default

            value. -->

        <level name="${jboss.server.log.threshold:DEBUG}" />

        <handlers>

            <handler-ref name="MERCURIUS" />

        </handlers>

    </root-logger>

 

</logging>

{code}

 

Since I use the /server/default/ configuration, I changed the logmanager-jboss-beans.xml in the /server/default/deployers/jboss-logging.deployer/META-INF directory. These are the lines relative to the per classloader context selector. I have added a link in the comment relative to the known problem:

 

{code:xml}

<!--

     ~ These two beans define the per-classloader log context selector which allows per-deployment logging.  Since

     ~ enabling this feature may have a performance impact in certain cases, it's started up lazily (on demand)

     ~ when a separate log context is defined in a user deployment.

     -->

   <!-- @Pasquale: modified according to https://issues.jboss.org/browse/JBAS-9407 -->

   <!-- Workaround for JBoss Logging bug http://community.jboss.org/message/587287#587287 -->

<bean name="JBossLogManagerContextSelectorService" class="org.jboss.logmanager.ClassLoaderLogContextSelector"/>

 

   <bean name="OnDemandJBossLogManagerContextSelectorService" class="org.jboss.logmanager.LogContextSelectorService">

      <property name="selector">

         <inject bean="JBossLogManagerContextSelectorService"/>

      </property>

   </bean>

{code}

 

The classes I deploy on the AS refer to the JBoss Logger. For instance, my Helper.java uses Logger like this:

{code:java}

import org.jboss.logging.Logger;

private static final Logger log = Logger.getLogger(Helper.class);

{code}

 

You'll probably get a NPE when stopping your AS, read here:

https://issues.jboss.org/browse/JBAS-9446?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel