Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typeHandlersPackage scanning not working when custom TypeHandler is in other (nested) jar than the SqlSessionFactoryBean definition #325

Closed
michalaron opened this issue Jan 5, 2015 · 13 comments
Assignees
Labels
enhancement Improve a feature or add a new feature
Milestone

Comments

@michalaron
Copy link

We have a multi-module project with the following structure:

batch.jar (created using Spring boot)
--persistenceContext.xml (containing definition SqlSessionFactoryBean - see bellow)
--package1/package2/Mapper1.xml (specific mapper for batch module)
--package1/package2/Mapper1.java (specific mapper for batch module)
--lib/persistence-module.jar
--lib/persistence-module.jar/package3/package4/Mapper2.xml (common mapper re-used by multiple modules)
--lib/persistence-module.jar/package3/package4/Mapper2.java (common mapper re-used by multiple modules)
--lib/persistence-module.jar/package3/package5/typehandler/DateTimeHandler.java (common typehandler re-used by multiple modules)

relevant parts of the persistenceContext.xml (placed in the OUTER jar file):

<!-- define the SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="typeHandlersPackage" value="package3.package5.typehandler" />
</bean>

<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="package1.package2, package3.package4" />
</bean>

The mapper scanning works without any problems (even for the Mapper2, which is placed in the inner jar file).
But typehandler scanning does not work correctly - the custom type handler DateTimeHandler.java is not found by myBatis in this configuration. I think the problem is on line 202 in org.apache.ibatis.io.DefaultVFS.java:

url = new URL(url.getFile());

This causes the URL in the form of "jar:file:batch.jar!/lib/persistence-module.jar/package3/package5/typehandler" is transformed to "file:batch.jar!/lib/persistence-module.jar/package3/package5/typehandler" and Exception is thrown on attempt to read from this URL on line 288 (isJar method).

Workaround I had to use was to duplicate the DateTimeHandler.java and put it to the outer jar (batch.jar) as well + change the spring context so now the scanned type handler package is the one in the outer jar. But this solution is of course not ideal and I definitely consider this as a bug (especially because rest of the mybatis configuration - i.e. the mapper scanning is working normally even for the mappers placed in the INNER jar).
Also I think the "swallowing" of the Exception in DefaultVFS.isJar() without any logging is not very good and it took me some time debugging the sources before finding the cause of this problem (and obviously the comment "Failure to read the stream means this is not a JAR" is not correct).

@michalaron
Copy link
Author

We are using version 3.2.8

@s17t
Copy link

s17t commented Jan 9, 2015

I tried to upgrade from 3.2.3 to 3.2.8 and I have missing typehandler errors too. My project's structure is similar. I tried 3.2.7 and 3.2.4 also but same errors occurred.

I reverted back to 3.2.3 and now works.

@harawata
Copy link
Member

harawata commented Apr 5, 2015

There seems to be no logical change in org.apache.ibatis.io.DefaultVFS since 3.2.3.
I'm not familiar with Spring Boot, could someone provide an example .jar or a project?

@making
Copy link

making commented May 19, 2015

👍

@kazuki43zoo
Copy link
Member

haljik pushed a commit to system-sekkei/isolating-the-domain that referenced this issue Jun 25, 2015
@harawata
Copy link
Member

@kazuki43zoo
Thank you very much for your time! I will look into it.

@harawata
Copy link
Member

According to the Spring Boot documentation, Java does not provide any standard way to load nested jar files and Spring Boot uses its own mechanism for it.

As these application server specific behavior should be handled with a custom VFS implementation, I have added a new setting 'vfsImpl' to make the VFS implementation configurable.

<settings>
  <setting name="vfsImpl" value="com.mydomain.vfs.SpringBootExecutableJarVFS" />
</settings>

This change should be available in the latest 3.4.0-SNAPSHOT.
I also sent a PR to Kazuki's repo as a demo of this fix.
Comments are welcome.


To @s17t
As the issue reported here is reproducible with MyBatis 3.2.3 as well, it may be different from your problem.
I suggest you to open a new ticket with the details (attaching a demo project would be helpful).

@harawata
Copy link
Member

Stripes guys came up with a few custom VFS implementations supporting Spring-Boot's executable JAR.
Please visit this issue and help them improve it.
Thank you!

@harawata
Copy link
Member

Just a heads up.
mybatis-spring-boot 1.0.1, by default, will use the custom VFS.
Please try the snapshot!

@kazuki43zoo
Copy link
Member

👍

I tried mybatis-spring-boot 1.0.1-SNAPSHOT. It's a very nice !!

Verifying application is
https://github.com/kazuki43zoo/spring-boot-mybatis-sample/tree/mybatis-spring-boot .

@harawata
Copy link
Member

Thanks for the feedback and the link, @kazuki43zoo !
Glad to know it's working 😄

@julnamoo
Copy link

Sorry to dive late.
Is this problem fixed in released mybatis-spring-boot:1.0.1?

@harawata
Copy link
Member

@julnamoo It should be =)
If you found a problem, please create a new issue on mybatis-spring-boot's tracker.

pulllock pushed a commit to pulllock/mybatis-3 that referenced this issue Oct 19, 2023
pulllock pushed a commit to pulllock/mybatis-3 that referenced this issue Oct 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve a feature or add a new feature
Projects
None yet
Development

No branches or pull requests

6 participants