Table of contents
1.
Introduction🗒️
2.
Legacy databases and keyed tables
3.
Distributed transaction 
4.
Frequently Asked Questions
4.1.
What is the web2py framework?
4.2.
Can web2py be used with Python 3?
4.3.
What is meant by Web application framework?
4.4.
For what purpose is Web2py used?
4.5.
Which is preferable, Django or web2py?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Legacy databases and Distributed transactions in web2py

Author yuvatimankar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction🗒️

web2py is a free, open-source web framework for  Agile development that implies database-driven web apps; web2py is written and programmable in Python. web2py is a framework for full-stack; it comprises all the components a developer requires to build a fully functional web application.

web2py

web2py follows MVC (Model-View-Controller) pattern for running web applications. The model here is part of the application, which includes the logic of the data, the view here is a part of the application that helps render the data display to end-users, and the controller here is a part of the application that handles user interaction. 

Legacy databases and keyed tables

Legacy databases are commonly referred to as a database that has been in use for several years and is unsuitable for modern apps and environments. The existence of a legacy database presents multiple challenges to an organization, according to the need to access and integrate the older data. A company may decide to leave the database and its applications if legacy data are required primarily as an archive.

Legacy database and keyed table

Web2py can connect to legacy databases in some conditions:

The simplest way is when these conditions are met:

  • Each table must have an unrepeated auto-increment integer field called id
  • Records must be referenced, particularly using the id field

 

When retrieving an existing table, i.e., a table that web2py does not create in the ongoing application, we need always to set migrate-false.

If the legacy table consists of an auto-increment integer field, but it is not called id, still we2py can access it. Still, the table definition must declare the auto-increment Field with type ‘id’ (i.e., by using Field (‘...’,’ id’). At last, if the legacy table uses a primary key which is not an auto-increment id field, then it is possible to use a “keyed table”; for example:

db.define_table('account',
                Field('accnum', 'integer'),
                Field('acctype'),
                Field('accdesc'),
                primarykey=['accnum', 'acctype'],
                migrate=False)

 

primarykey is a list of the names of the Field that makes the primary key. All primarykey fields consist of a NOT NULL set even if it is not specified. Keyed tables can only refer to other keyed tables. Referencing fields must use the format reference tablename.fieldname. The function update_record is not available for Rows of keyed tables. 

At the time of writing, we cannot make sure that the primarykey attribute works with every existing legacy table and supported database backend. For clarity, we recommend creating a db view that has an auto-increment id field. 

Distributed transaction 

Distributed Transactions

A distributed transaction is a database transaction in which multiple network hosts are involved. Commonly, hosts anticipate transactional resources, while the transaction manager is accountable for managing and creating a global transaction that encompasses all operations against such resources. 

Let’s assume we have two or more connections to different PostgreSQL databases, for example:

db_a = DAL('postgres://...')
db_b = DAL('postgres://...')

 

In our models and controller, we can commit them concurrently with:

DAL.distributed_transaction_commit(db_a, db_b)


When fails, this function rolls back and raises an Exception.

In controllers, when one action returns, if we have two distinct connections and we do not call the above function, web2py commits them distinctively. This means there is a possibility that one of the commits fails and one succeeds. And this is prevented by the distributed transaction.

Frequently Asked Questions

What is the web2py framework?

Web2py, written in Python and programmable in Python, is a free, open-source online framework for agile development that involves database-driven web applications.

Can web2py be used with Python 3?

Web2py functions on Python 2.7, Python 3, with CPython (the C implementation), and PyPy (Python written in Python).

What is meant by Web application framework?

A software framework known as a web application framework (WAF) is made to facilitate the creation of web applications, which include web resources, web services, and web APIs.

For what purpose is Web2py used?

Web2py is mainly used to program dynamic websites using Python as a programming language. It contains all the tools, components, and APIs essential to building a fully functional web application.

Which is preferable, Django or web2py?

Due to its smaller size, short learning curve, and lack of project-level configuration files, Web2py is preferred over Django. Compared to PHP-based frameworks and Java-based frameworks, Web2py has a significantly more explicit syntax than PHP-based and Java-based frameworks, making this application easier to comprehend, maintain, and create.

Conclusion

In this article, we discussed the web2py introduction, and then we saw the Legacy database and distributed transactions in web2py. We started with the introduction of web2py, then we saw Legacy database and Distributed transaction and then distributed transaction in detail.

Also, visit our Guided Path in  Coding Ninjas Studio to learn about Web2py. See our Interview Experience Section and interview bundle for placement preparations if you are preparing for an interview. Upskill yourself in PythonKivyBackend Web TechnologiesSQL, MongoDB, Data Structures and Algorithms, JavaScript,  System Design, and much more!. Please upvote our blogs if you find them engaging and helpful! 

Here are some useful links Web2pyKivyRuby vs. Python. You can also refer to the Web2py IntroductionWeb2py InstallationWeb2py ApplicationWeb2py ToolWeb2py Capacity Planning, and Troubleshooting in Web2py.   

Wish you all the best for your future adventures and Happy Coding

Conclusion
Live masterclass