Announcement Announcement Module
Collapse
No announcement yet.
Trasactions in Sping with out Database Manipulation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trasactions in Sping with out Database Manipulation

    Hi Folks,

    I am new to Spring Framework. I have one basic question. I dont have any database operations in my class. So not even configuring the datasource can i make my method trasactional ?. If so can you please give me some good example of the same.

    Please let me know if you guys need any more information.

    Thanks in advance,
    Srikanth

  • #2
    If you aren't working with a database, what have you got to provide the transaction? What is it your trying to do?

    Comment


    • #3
      Trasactions in Sping with out Database Manipulation

      i 'm trying to test a simple java class which has Simple Sytem.out.println in the methods. And i have applicationContext.xml file, in which i have created a JTATransactionManager and given that reference in the What i feel is that transactions are not only relate to database work. So trying to test the same.


      applicationContext.xml looks like this

      <beans>
      <bean id="transactionManager"
      class="org.springframework.transaction.jta.JtaTran sactionManager">
      </bean>
      <bean id="accountManagerTarget" class="DefaultAccountManager">
      </bean>

      <bean id="accountManager"
      class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
      <property name="transactionManager">
      <ref bean="transactionManager"/>
      </property>
      <property name="target">
      <ref local="accountManagerTarget"/>
      </property>
      </bean>

      </beans>


      public class DefaultAccountManager {

      public int count() {
      // TODO Auto-generated method stub
      System.out.println("DefaultAccountManager.java : count");
      return 0;
      }

      public void deposit(int accountId, BigDecimal amount) {
      // TODO Auto-generated method stub
      System.out.println("DefaultAccountManager.java : deposit");

      }

      public void insert(int account) {
      // TODO Auto-generated method stub
      System.out.println("DefaultAccountManager.java : insert");

      }

      public void transfer(int sourceAccount, int targetAccount, BigDecimal amount) {
      // TODO Auto-generated method stub
      System.out.println("DefaultAccountManager.java : transfer");

      }

      }


      Thanks,
      Srikanth

      Comment


      • #4
        You could create a mock DataSource that would let you run the code. I still don't understand what you are trying to achieve however. If you have a TransactionManager and DataSource configured but you don't have a database what's the point? Maybe the answer is simple, but it's hard to understand what you are trying to do.

        Comment


        • #5
          Yes i do understand what your traying to convey to me. But i am testing whether i can make a class work in transaction without having any database operartions. This is just for understanding purpose. i read some where about this in Pro Spring book. Following paragraph copied from pro spring book. Not sure what else we can do with transactions.

          "The common belief is that transactions only relate to database work. The simplistic view of a transaction is begin transaction—SQL updates, deletes, and so on—followed by commit/rollback, but there's much more to transactions than this!"

          Comment


          • #6
            Well the quote is quite right, there are other things you can do within a transaction. All I was trying to understand was what you're trying to do. I haven't read the book so I can't really comment. As I said previously though you could mock a DataSource to test it.

            Comment


            • #7
              Ok. Can i know what all other things we can do with transactions.

              Thanks for reply,

              Srikanth

              Comment


              • #8
                Karl,

                One more thing.I have a class called AllocationService which is starting point of my applications. I want to use spring to setup declarative transactions within these services classes.The transaction manager associated with this needs to be a JTA 2-phase commit transactions manager, which I believe is specified in Spring as a JTATransactionManager (which only works inside a full J2EE container).


                Thanks,
                Srikanth

                Comment


                • #9
                  There are lots of articles out there that talk about this. Databases are one aspect, but there are others e.g. JMS, file system, etc....
                  http://www.onjava.com/pub/st/18

                  Comment


                  • #10
                    I've not had to use two-phase commit with Spring yet. There are quite a few articles out there however. I'm pretty sure the Spring site has some links, the forum has discussed it many times and I'm sure googling it will yield yet more results.

                    Comment


                    • #11
                      Transactions only make sense with transactional resources, as Karl says. Transactional resources are typically relationals, but they can also be message queues, XA-enabled legacy systems etc...

                      I suggest that f you want to test transactions, you use an embedded database such as HSQL. It's trivial to set up (look at the Spring examples) and will show you the requisite behavior. If you don't have any transactional resources, your JTA implementation may not end up doing anything.

                      Comment

                      Working...
                      X