10 Replies Latest reply on Oct 9, 2013 5:30 AM by Radim Hanus

    Oracle database connection in MDB

    Kaushik Parasam Newbie

      Hi, How can I connect to Oracle database in a MDB ? I've bounded a ojdbc oralce driver on JBoss. I tried with Resource injection in MDB, but doesn't seem to work, or I'm missing something. Any inputs would help !

        • 1. Re: Oracle database connection in MDB
          Radim Hanus Expert

          define mappedName of configured datasource:

           

          @Resource(mappedName="java:/OraDB")

          private DataSource ds;

           

          or use JPA persistence context:

           

          @PersistenceContext(unitName = "anOraDBPU")

          private EntityManager em;

          1 of 1 people found this helpful
          • 2. Re: Oracle database connection in MDB
            Kaushik Parasam Newbie

            I tried with mappedName, but DataSource is still null. The JNDI name in the domain.xml is jndi-name="java:/jdbc/MYDS" and the driver class is oracle.jdbc.driver.OracleDriver. In MDB, I've

            @Resource(mappedName="java:/jdbc/MYDS")

            private DataSource ds;

             

            Any thoughts ?!

            • 3. Re: Oracle database connection in MDB
              Radim Hanus Expert

              are there any errors/warns in server.log ?

              is your archive where the MDB is available correctly deployed ?

              • 4. Re: Oracle database connection in MDB
                Kaushik Parasam Newbie

                I've a few messages in a queue and getting the following exception upon start up of server ...abstract void javax.jms.MessageListener.onMessage(javax.jms.Message): javax.ejb.EJBTransactionRolledbackException. And the NullPointerException when trying to do ds.getConnection() [in the OnMessage() MDB method]. This might not be the right way, but I would like to test a Oracle transaction through MDB. I might be missing something, recently started on coding with MDB.

                • 5. Re: Oracle database connection in MDB
                  Kaushik Parasam Newbie

                  Still not able to get it right. Any help would be appreciated. Thanks !

                  • 6. Re: Oracle database connection in MDB
                    Kaushik Parasam Newbie

                    Even the @PostConstruct and @PreDestroy annotations doesn't seem to work. I'm using JBoss-7.1.1.Final.

                    • 7. Re: Oracle database connection in MDB
                      Kaushik Parasam Newbie

                      Could someone please update this thread. To give more context, I need to have a Oracle database transaction managed by container (or any other way ?). I've a JMS queue setup for sending messages from a set of clients and the MDB processes them. Along with this, I need to do an insert into a Oracle database. Container-manager transactions is possible using an MDB according to some articles online. So, I would like to know the right way to achieve this. Thanks !

                      • 8. Re: Oracle database connection in MDB
                        Radim Hanus Expert

                        if injection field is null and both PostConstruct and PreDestroy are not called then your bean is not deployed and therefore not managed by application server

                        again check your log for deployment problems to be sure that corresponding archive with MDB inside is installed in jboss

                        • 9. Re: Oracle database connection in MDB
                          Kaushik Parasam Newbie

                          Not sure its a deployment problem, I don't see any errors except for this one. I've got the MDB working to pick up messages from a queue and process them. The ejbCreate(), ejbRemove(), setMessageContext(arg) and OnMessage() are called by container as expected. Just to make sure I'm doing it right so far, here're my configs:

                          jboss.xml:

                          <jboss>

                            <enterprise-beans>

                              <message-driven>

                                <ejb-name>MyMDB</ejb-name>

                                <destination-jndi-name>queue/testQueue</destination-jndi-name>

                              </message-driven>

                            </enterprise-beans>

                          </jboss>

                           

                          ejb-jar.xml:

                          <ejb-jar>

                            <enterprise-beans>

                              <message-driven>

                                <ejb-name>MyMDB</ejb-name>

                                <ejb-class><classpath>/MyMDB</ejb-class>     

                                <transaction-type>Container</transaction-type>

                                <message-destination-type>javax.jms.Queue</message-destination-type>   

                               <activation-config>

                                  <activation-config-property>

                                    <activation-config-property-name>destinationType</activation-config-property-name>

                                    <activation-config-property-value>javax.jms.Queue</activation-config-property-value>

                                  </activation-config-property>

                                  <activation-config-property>

                                    <activation-config-property-name>destination</activation-config-property-name>

                                    <activation-config-property-value>queue/testQueue</activation-config-property-value>

                                  </activation-config-property>

                                  <activation-config-property>

                                    <activation-config-property-name>acknowledgeMode</activation-config-property-name>

                                    <activation-config-property-value>Auto-acknowledge</activation-config-property-value>

                                  </activation-config-property>        

                                </activation-config>     

                              </message-driven>

                            </enterprise-beans>

                          </ejb-jar>

                           

                           

                          MDB code:

                          public class MyMDB implements MessageDrivenBean, MessageListener {

                              private Connection connection;

                             

                              @Resource(mappedName="java:/MYDS")

                              private DataSource dataSource;

                             

                              public MyMDB() {}

                               

                              public void ejbCreate() throws EJBException {System.out.println("Create !");}

                             

                              @Override

                              public void ejbRemove() throws EJBException {System.out.println("Remove !");}

                           

                              @Override

                              public void setMessageDrivenContext(MessageDrivenContext arg0)

                              throws EJBException {System.out.println("Context !");}

                           

                              @PostConstruct

                              public void initialize() {

                              try {

                                      System.out.println("Initialize !");

                                  connection = dataSource.getConnection();

                              } catch (SQLException sqle) {

                                  sqle.printStackTrace();

                                  }

                              }

                           

                              @PreDestroy

                              public void cleanup() {

                              try {

                                  System.out.println("Cleanup !");

                                  connection.close();

                                  connection = null;

                              } catch (SQLException sqle) {

                                  sqle.printStackTrace();

                              }

                              }

                               

                              public void onMessage (Message message) {/* The logic whatever I've here works ! */}

                          • 10. Re: Oracle database connection in MDB
                            Radim Hanus Expert

                            yep I see it's ejb 2.x code where annotation are not supported

                            comment out xml files and use @MessageDriven annotation and then it should work fine :-)