15
BLG 411E Software Engineering Recitation 4 Logging Log4j ORM Hibernate References BLG 411E – Software Engineering Recitation Session 4 Application Logging and Object-Relational Mapping Atakan Aral, Bilge Süheyla Akkoca 02.12.2014

Software Engineering - RS4

Embed Size (px)

DESCRIPTION

BLG 411E – Software Engineering Recitation Session 4 Application Logging and Object-Relational Mapping

Citation preview

Page 1: Software Engineering - RS4

BLG 411ESoftware

Engineering

Recitation 4

LoggingLog4j

ORMHibernate

References

BLG 411E – Software EngineeringRecitation Session 4

Application Logging and Object-Relational Mapping

Atakan Aral, Bilge Süheyla Akkoca

02.12.2014

Page 2: Software Engineering - RS4

BLG 411ESoftware

Engineering

Recitation 4

LoggingLog4j

ORMHibernate

References

Outline

1 LoggingLog4j

2 ORMHibernate

3 References

Page 3: Software Engineering - RS4

BLG 411ESoftware

Engineering

Recitation 4

LoggingLog4j

ORMHibernate

References

Application LoggingIntroduction

Definition

Recording the events which happen during a softwareexecution.

Logging allows to:audit important items,analyze application use,trace errors.

Performance is the most important consideration.

Page 4: Software Engineering - RS4

BLG 411ESoftware

Engineering

Recitation 4

LoggingLog4j

ORMHibernate

References

Application LoggingLogging Levels

OFF No loggingFATAL Only errors that cause the application to abort

ERROR Critical errors that prevent the use case fromcontinuing

WARN Other errors and exceptions where processesmay still continue, i.e. “Current dataunavailable, using cached values”

INFO Life cycle events, completion of use cases, i.e.“[Who] booked ticket from [Where] to [Where]”

DEBUG All other (sub) eventsTRACE All method invocations with parameter and

return values

Page 5: Software Engineering - RS4

BLG 411ESoftware

Engineering

Recitation 4

LoggingLog4j

ORMHibernate

References

Application LoggingTips

Apply logging levels appropriatelyAvoid side effects (no impact on the application’sbehavior)Log for easy reading and easy parsingInclude both data and description, no magic numbersor characters (i.e. &&&)Use a logging pattern (i.e. %d{HH:mm:ss.SSS}%-5level [%thread][%logger{0}] %m%n)Maximize logging for external systemsMaximize logging when in trouble, not otherwiseAvoid excessive string concatenation, use parametrizedlogging methodsDo not log sensitive information

Page 6: Software Engineering - RS4

BLG 411ESoftware

Engineering

Recitation 4

LoggingLog4j

ORMHibernate

References

Log4jIntroduction

Definition

A Java-based logging utility which provides performanceimprovements and advanced filtering.

import org . apache . logg ing . l o g 4 j . LogManager ;import org . apache . logg ing . l o g 4 j . Logger ;

public class Hel loWorld {private s t a t i c f i n a l Logger l

= LogManager . getLogger ( " Hel loWorld " ) ;public s t a t i c void main ( S t r i n g [ ] args ) {

l . i n f o ( " Hel lo , World ! " ) ;l . e r r o r ( " Mission f a i l e d ! " ) ;

}}

Page 7: Software Engineering - RS4

BLG 411ESoftware

Engineering

Recitation 4

LoggingLog4j

ORMHibernate

References

Object-Relational MappingDefinitions

Persistence Application’s data to outlive theapplications process, state of objectsto live beyond the scope of the JVM sothat the same state is available later

DMSs Although ODBMSs are emerging,RDBMSs are still the most popularones.

Paradigm mismatch Object models (i.e. Java) andrelational models (i.e. SQL) do notwork very well together. See the nextslide for the details.

ORM A programming technique forconverting data between the twoincompatible type systems

Page 8: Software Engineering - RS4

BLG 411ESoftware

Engineering

Recitation 4

LoggingLog4j

ORMHibernate

References

Object-Relational MappingObject-relational impedance mismatch

RDBMSs represent data in a tabular format whereobject-oriented languages represent it as an inter-connected graph of objects. Mismatch problems:Granularity Object model usually has more classes

than the number of corresponding tablesin the database.

Subtypes Inheritance in OOP does not exist inRDBMSs.

Identity OOP supports identity (a == b) andequality (a.equals(b)) while RDBMSs onlysupport identity (the primary key).

Association References vs. foreign keysNavigation In OOP, navigation is from one association

to another, walking the object network.That is inefficient in RDBMSs, so JOINsare used.

Page 9: Software Engineering - RS4

BLG 411ESoftware

Engineering

Recitation 4

LoggingLog4j

ORMHibernate

References

HibernateIntroduction

Definition

Hibernate is an ORM library for Java that solves object-relational impedance mismatch problems by replacing directpersistence-related DB access with high-level objecthandling functions.

Allows developer to concentrate on Java code insteadof complex SQL statements.Reduces the lines of code, makes the system moreunderstandable and easier to maintain.Abstracts the application away from the underlying SQLdatabase and dialect, fosters portability.

Page 10: Software Engineering - RS4

BLG 411ESoftware

Engineering

Recitation 4

LoggingLog4j

ORMHibernate

References

HibernateHibernate mapping file

Class–TableProperty–IdProperty–Column

Types and column names are optional.

<hibernate−mapping><c lass name=" Product " t ab l e ="PRODUCTS">

< i d name=" produc t Id " column=" p id " ><generator c lass=" assigned " / >

< / i d ><proper ty name="proName" column="pname" / ><proper ty name=" p r i ce " / ><proper ty name=" date " type=" timestamp "

column=" c rea t ion_da te " / >< / c lass>

< / h ibernate−mapping>

Page 11: Software Engineering - RS4

BLG 411ESoftware

Engineering

Recitation 4

LoggingLog4j

ORMHibernate

References

HibernateHibernate configuration file

JDBC connection information (connection.driver_class,connection.username, connection.password,connection.url)Number of connections (connection.pool_size)SQL variant (dialect)DB schema generation (hbm2ddl.auto)Mapping persistent classes (mapping)

Page 12: Software Engineering - RS4

BLG 411ESoftware

Engineering

Recitation 4

LoggingLog4j

ORMHibernate

References

HibernateHibernate configuration file

<hibernate−c o n f i g u r a t i o n ><session−f a c t o r y >

<proper ty name=" connect ion . d r i v e r _ c l a s s ">orac le . jdbc . d r i v e r . Orac leDr iver

< / p roper ty><proper ty name=" connect ion . u r l ">

jdbc:orac le: th in :@www . java4s . com:1521:XE< / p roper ty><proper ty name=" connect ion . user ">user< / p roper ty><proper ty name=" connect ion . password ">

password< / p roper ty>

<proper ty name=" show_sql "> t rue < / p roper ty><proper ty name=" d i a l e c t ">

org . h iberna te . d i a l e c t . Orac leD ia lec t< / p roper ty><proper ty name=" hbm2ddl . auto ">update< / p roper ty>

<mapping resource=" product .hbm. xml " / >< / session−f a c t o r y >

< / h ibernate−c o n f i g u r a t i o n >

Page 13: Software Engineering - RS4

BLG 411ESoftware

Engineering

Recitation 4

LoggingLog4j

ORMHibernate

References

HibernateSample program code

import org . h iberna te . ∗ ;import org . h iberna te . c fg . ∗ ;

public class ClientForSave {public s t a t i c void main ( S t r i n g [ ] args ) {

Con f i gu ra t i on c fg = new Con f i gu ra t i on ( ) ;c fg . con f igu re ( " h iberna te . c fg . xml " ) ;SessionFactory f a c t o r y = cfg . bu i ldSess ionFactory ( ) ;Session session = f a c t o r y . openSession ( ) ;

Product p = new Product ( ) ;p . se tProduc t Id ( 1 0 1 ) ;p . setProName ( " iPhone " ) ;p . se tP r i ce (25000) ;p . setDate (new Date ( ) ) ;

session . beg inTransact ion ( ) ;session . save ( p ) ;session . ge tTransact ion ( ) . commit ( ) ;session . c lose ( ) ;f a c t o r y . c lose ( ) ;

}}

Page 14: Software Engineering - RS4

BLG 411ESoftware

Engineering

Recitation 4

LoggingLog4j

ORMHibernate

References

HibernateSample program code

session = sess ionFactory . openSession ( ) ;session . beg inTransact ion ( ) ;

L i s t r e s u l t = session . createQuery ( " from PRODUCTS" ) . l i s t ( ) ;

for ( Product p : ( L i s t <Product >) r e s u l t ) {System . out . p r i n t l n ( " Product ( "

+ p . se tProduc t Id ( ) + " ) : " + p . setProName ( ) ) ;}

session . ge tTransact ion ( ) . commit ( ) ;session . c lose ( ) ;

Page 15: Software Engineering - RS4

BLG 411ESoftware

Engineering

Recitation 4

LoggingLog4j

ORMHibernate

References

References and Further Reading

http://www.javacodegeeks.com/2011/01/10-tips-proper-application-logging.html

http://blogg.kantega.no/logging-in-java-with-users-in-mind/

http://logging.apache.org/log4j/2.x/

http://www.agiledata.org/essays/impedanceMismatch.html

http://hibernate.org/orm/

http://www.java4s.com/hibernate/hibernate-hello-world-program-saving-an-objcet/