Tuesday, August 23, 2011

slf4j issues on JEE projects

The following errors have something in common - the slf4 library.




Each of them are commented, and probably you can find if your problem fits to one of them.




Example #1
SLF4J: Class path contains multiple SLF4J bindings.Criação inicial do objeto SessionFactory falhou. Erro: java.lang.NoSuchMethodError: org.slf4j.impl.StaticLoggerBinder.getSingleton()Lorg/slf4j/impl/StaticLoggerBinder;

This is really simple to solve.
Search for two versions of slf4 libraries, like this:
project/lib/slf4j-api-1.6.1.jar
project/lib/slf4j-api.jar
Delete one of them, preferably the one which does not have the version number.

Example #2 - Running on Eclipse
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/servers/jboss-5.1.0.GA/common/lib/slf4j-jboss-logging.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/servers/jboss-5.1.0.GA/client/slf4j-jboss-logging.jar!/org/slf4j/impl/StaticLoggerBinder.class]



If the jar files listed above are not inside the project, try to find if you have some duplicated reference on the project's properties, like Build Path (Source,Project and Library tabs), Targeted Runtimes, etc.


For instance, if you have a reference to another project that also contains the same libraries, they'll conflict. The pictures below show a conflict between the project and the referenced project, the one added on Project tab.
You can have the same issue on the Library tab, if you added libraries from a server, for example.
Following the red arrows. Click over the picture to zoom.







The eclipse console will show an error message like this:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/alsdias/work/dev/servers/jboss-5.1.0.GA/common/lib/slf4j-jboss-logging.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/alsdias/work/dev/servers/jboss-5.1.0.GA/client/slf4j-jboss-logging.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.



If everything seems to be fine, but the problem is still there, the fastest way to solve a "ghost" reference is to create a new project, importing the code again.

Example #3
Hibernate: select nextval ('hibernate_sequence')
Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.event.PreInsertEvent.getSource()Lorg/hibernate/event/EventSource;
at org.hibernate.validator.event.ValidateEventListener.onPreInsert(ValidateEventListener.java:172)



Usually happens due incompatibilities with the project's library and the slf4 version.
Using annotations, this problem was solved switching these versions:
slf4j-api-1.5.10.jar
slf4j-simple-1.5.0.jar

by this one:
slf4j-api-1.6.1.jar


Example #4
17:39:03,260 INFO Configuration:1547 - Configured SessionFactory: null
...
anything else here



The 4th example leads us to a false conclusion - that the problem occurred due a null SessionFactory.
This is a common message and it is usually logged by the application during successful procedures, as shown in the example below,
which shows the output generated by a simple insertion of an entity on the database table.
Sometimes, it's the last message to be generated before the exception occurrence, and so, we may think that it is the cause.

In this case, check the version of the slf4.jar used on the project.
If using annotation, try this version:
slf4j-api-1.6.1.jar

Conclusion:
If you're getting these kinds of errors, try another version for your slf4..jar  before trying anything else.

For more details about slf4 error/warning messages, check http://www.slf4j.org/codes.html#StaticLoggerBinder

Example of successful procedure containing the "SessionFactory: null" message:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
17:39:02,870 INFO Environment:514 - Hibernate 3.2.6
17:39:02,876 INFO Environment:547 - hibernate.properties not found
17:39:02,879 INFO Environment:681 - Bytecode provider name : cglib
17:39:02,883 INFO Environment:598 - using JDK 1.4 java.sql.Timestamp handling
17:39:02,962 INFO Configuration:1432 - configuring from resource: hibernate.cfg.xml
17:39:02,963 INFO Configuration:1409 - Configuration resource: hibernate.cfg.xml
17:39:03,260 INFO Configuration:1547 - Configured SessionFactory: null
17:39:03,576 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
17:39:03,576 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20
17:39:03,577 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
17:39:03,588 INFO DriverManagerConnectionProvider:80 - using driver: org.postgresql.Driver at URL: jdbc:postgresql://localhost:5432/getit
17:39:03,589 INFO DriverManagerConnectionProvider:86 - connection properties: {user=postgres, password=****}
17:39:04,480 INFO SettingsFactory:89 - RDBMS: PostgreSQL, version: 8.3.7
17:39:04,481 INFO SettingsFactory:90 - JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 8.4 JDBC3 (build 701)
17:39:04,661 INFO Dialect:152 - Using dialect: org.hibernate.dialect.PostgreSQLDialect
17:39:04,692 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
17:39:04,706 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
17:39:04,707 INFO SettingsFactory:143 - Automatic flush during beforeCompletion(): disabled
17:39:04,708 INFO SettingsFactory:147 - Automatic session close at end of transaction: disabled
17:39:04,708 INFO SettingsFactory:154 - JDBC batch size: 15
17:39:04,708 INFO SettingsFactory:157 - JDBC batch updates for versioned data: disabled
17:39:04,710 INFO SettingsFactory:162 - Scrollable result sets: enabled
17:39:04,711 INFO SettingsFactory:170 - JDBC3 getGeneratedKeys(): enabled
17:39:04,711 INFO SettingsFactory:178 - Connection release mode: auto
17:39:04,713 INFO SettingsFactory:205 - Default batch fetch size: 1
17:39:04,713 INFO SettingsFactory:209 - Generate SQL with comments: disabled
17:39:04,717 INFO SettingsFactory:213 - Order SQL updates by primary key: disabled
17:39:04,717 INFO SettingsFactory:217 - Order SQL inserts for batching: disabled
17:39:04,718 INFO SettingsFactory:386 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
17:39:04,721 INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory
17:39:04,722 INFO SettingsFactory:225 - Query language substitutions: {}
17:39:04,722 INFO SettingsFactory:230 - JPA-QL strict compliance: disabled
17:39:04,723 INFO SettingsFactory:235 - Second-level cache: enabled
17:39:04,723 INFO SettingsFactory:239 - Query cache: disabled
17:39:04,723 INFO SettingsFactory:373 - Cache provider: org.hibernate.cache.NoCacheProvider
17:39:04,729 INFO SettingsFactory:254 - Optimize cache for minimal puts: disabled
17:39:04,730 INFO SettingsFactory:263 - Structured second-level cache entries: disabled
17:39:04,741 INFO SettingsFactory:283 - Echoing all SQL to stdout
17:39:04,741 INFO SettingsFactory:290 - Statistics: disabled
17:39:04,741 INFO SettingsFactory:294 - Deleted entity synthetic identifier rollback: disabled
17:39:04,744 INFO SettingsFactory:309 - Default entity-mode: pojo
17:39:04,744 INFO SettingsFactory:313 - Named query checking : enabled
17:39:04,817 INFO SessionFactoryImpl:161 - building session factory
17:39:05,361 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into public.test (name, id) values (?, ?)

No comments:

Post a Comment

eclipse: java: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" or Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

  >PROBLEM Using Eclipse, you try to run a simple logging test using "org.slf4j.Logger" like the sample below: package Test; im...