Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

I am trying to create a new Attachment for a Confluence page but have been unsuccessful


Using the Java API in ScriptRunner/groovy I am trying to retrieve all attachments for a page and then create a new one if it currently does not exists.

Everything is working so far, except the creation of the new attachment.  Clearly, I'm missing something and community insight would be super helpful! I'm fairly new to Java/ScriptRunner/Groovy programming.

Attachment newFile = new Attachment("filename.csv", "text/csv", 6014, "new file");

Per:, that should be all that is needed to construct a new Attachment object. 

Once the attachment is created I would use AttachmentManager to save the file to the page with content from InputStream/ByteArray.

Does the file/content actually need to exist on the server somewhere before I can create the attachment and save to the page?

However, I receive the following errors in the log. <snippet>

org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only at at at jdk.internal.reflect.GeneratedMethodAccessor80.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at at org.springframework.aop.framework.JdkDynamicAopProxy.invoke( at com.sun.proxy.$Proxy75.commit(Unknown Source)



2 answers

1 accepted

Hi @Matt , if you've not already worked this out, the issue would be that the Attachment needs to have a container reference (that is, a reference to the page it's being saved on) set to the page that you're uploading it to - see the exception note on the api

IllegalArgumentException - if attachment's content is not set


The code I've used that successfully uploads an attachment looks like

def final pageTitle = 'myPage';
def final spaceKey = 'KEY';

def destinationPage = pageManager.getPage spaceKey, pageTitle;
def newAttachment = new Attachment();
newAttachment.with {
fileName = 'my file name'
mediaType = 'mime type'
fileSize = content.length
versionComment = 'a comment'
container = destinationPage
attachmentManager.saveAttachment(newAttachment, null, new ByteArrayInputStream(content)

where content is a byte array.

Note this doesn't enable you to view the attachment on the page, but if you go to the page Attachments section, it'll be listed there. If you want to overwrite an existing attachment, you need to instead search for the existing attachment and pass it in as the previousVersionOfAttachment.

0 votes
Hasnae Atlassian Team May 17, 2020

Hello @Matt 

Not sure what is going on there, but I usually use `AttachmentService` from our java-api over the `AttachmentManager`

Some questions to help me understand what's going on with that specific API usage :

  • is there anything else in the logs around that transaction rollback ? a `caused by` stacktrace ?
  • which JDK and Confluence versions are you on ?

@Hasnae, thank you very much for you reply! 

A bit of background.  This is Confluence Server, 7.3.1 and this is being written through ScriptRunner so I am not sure what JDK but I'd assume it's in the 8.x family.

As stated in this article, the error thrown before the "org.springframework.transaction.UnexpectedRollbackException" exception may be the culprit.  

Looking at the logs, I see this before the "UnexpectedRollbackException":

2020-05-19 21:09:53,309 INFO [http-nio-8090-exec-454 url:/rest/scriptrun...heduledJob/preview username:] [onresolve.scriptrunner.runner.ScriptBindingsManager] call Attachment: GroupAdmin_audit_2020.csv v.1 (0) null

2020-05-19 21:09:53,314 ERROR [http-nio-8090-exec-454 url:/rest/scriptrun...heduledJob/preview ] [onresolve.scriptrunner.runner.ScriptBindingsManager] call Attachment content
-- referer: /plugins/servlet/scriptrunner/admin/jobs/edit/07a91bc6-58be-4aea-b31a-0acc9f2e5c8d?REF=EDACA9A69B5BE5EA19B534CAA0CB76C490997B3BD7A0F23D3E9100000004 | url: /rest/scriptrunner/latest/scheduled-jobs/ | traceId: 4b20be416167a207 | userName: | [814937b7-1d59-49a8-9f66-06963a8e334d]

2020-05-19 21:09:53,317 WARN [http-nio-8090-exec-454 url:/rest/scriptrun...heduledJob/preview username:] [confluence.impl.hibernate.ConfluenceHibernateTransactionManager] doRollback Performing rollback. Transactions:
->[PluginReadWriteTx]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT (Session #1888224564)
-- referer: /plugins/servlet/scriptrunner/admin/jobs/edit/07a91bc6-58be-4aea-b31a-0acc9f2e5c8d?REF=EDACA9A69B5BE5EA19B534CAA0CB76C490997B3BD7A0F23D3E9100000004 | url: /rest/scriptrunner/latest/scheduled-jobs/ | traceId: 4b20be416167a207 | userName: | [814937b7-1d59-49a8-9f66-06963a8e334d]


I've bolded the part which I believe is causing the issue. It appears the attachment is null. So now I'm back to the drawing board of creating a new file attachment for a page without first writing the file to the filesystem. 

Can the AttachmentService accomplish this?

Many Thanks!


Hasnae Atlassian Team May 19, 2020


I was hoping for this attachment being null error to surface again, so I can reproduce it consistently.

I have vague memories of "something somewhere" requiring a new hibernate transaction, which in this case means some objects are null. I will make some time this week/next to help come up with a workaround

Thank you so much for the clarification of how the code is executed, extremely useful to my team !

:thinking: I don't think the `AttachmentService` allows for that at the moment, looks like a good improvement to add to it.



Like Matt likes this

Any help formulating a work around would be wonderful! I tried to do this again with saving the file to the Confluence temp dir first and then uploading but I'm still seeing "org.springframework.transaction.UnexpectedRollbackException" error but it does not show the attachment being null. 


2020-05-20 21:11:05,963 ERROR [http-nio-8090-exec-472 url:/rest/scriptrun...heduledJob/preview ] [onresolve.scriptrunner.runner.ScriptBindingsManager] call Attachment content
-- referer: | url: /rest/scriptrunner/latest/scheduled-jobs/ | traceId: 97633fdaa713cb21 | userName: | [75ae8dc5-02c3-4272-9d0b-d052f95377d1]
Hasnae Atlassian Team May 21, 2020

Alright, I am starting my Friday just now, busy morning, but making time for it this afternoon.

Thanks for your patience !

Like Matt likes this

Hi @Hasnae, any luck on this? Thanks! 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Confluence Cloud

🎨 Add some visual life to your templates

Hi Atlassian Community, My name is Avni Barman, and I am a Product Manager on the Confluence Cloud team. Based on feedback from you, we are giving admins more power to create templates that a...

131 views 1 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you